<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.7.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.7.0" type="image/png" sizes="32x32"><meta name="baidu-site-verification" content="code-aMPs2J7ZPy"><meta property="og:type" content="website">
<meta property="og:title" content="星辰大海">
<meta property="og:url" content="http://bujiuzhi.gitee.io/index.html">
<meta property="og:site_name" content="星辰大海">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="不久">
<meta name="twitter:card" content="summary"><title>星辰大海</title><link ref="canonical" href="http://bujiuzhi.gitee.io/index.html"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.7.0"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement('script');
  hm.src = 'https://hm.baidu.com/hm.js?12b4347d0a1f32de969a2276a08cb466';
  hm.async = true;

  if (false) {
    hm.setAttribute('data-pjax', '');
  }
  var s = document.getElementsByTagName('script')[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":8},
  header: {"enable":true,"showOnPost":false,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"carbon","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">星辰大海</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content content-home" id="content"><section class="postlist"><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2023/03/28/Hadoop/">Hadoop</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2023-03-28</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2023-03-28</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="Hadoop"   >
          <a href="#Hadoop" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop" class="headerlink" title="Hadoop"></a>Hadoop</h1>
      
        <h2 id="Hadoop和Hadoop生态系统"   >
          <a href="#Hadoop和Hadoop生态系统" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop和Hadoop生态系统" class="headerlink" title="Hadoop和Hadoop生态系统"></a>Hadoop和Hadoop生态系统</h2>
      
        <h3 id="Hadoop生态系统"   >
          <a href="#Hadoop生态系统" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop生态系统" class="headerlink" title="Hadoop生态系统"></a>Hadoop生态系统</h3>
      <p>广义上来说，Hadoop通常是指一个更广泛的概念。</p>
<ul>
<li><p>  <strong>Zookeeper</strong>：是一个开源的分布式应用程序协调服务,基于zookeeper可以实现同步服务，配置维护，命名服务。</p>
</li>
<li><p>  <strong>Flume</strong>：一个高可用的，高可靠的，分布式的海量日志采集、聚合和传输的系统。</p>
</li>
<li><p>  <strong>Hbase</strong>：是一个分布式的、面向列的开源数据库, 利用Hadoop HDFS作为其存储系统。</p>
</li>
<li><p>  <strong>Hive</strong>：基于Hadoop的一个数据仓库工具，可以将结构化的数据档映射为一张数据库表，并提供简单的sql 查询功能，可以将sql语句转换为MapReduce任务进行运行。</p>
</li>
<li><p>  <strong>Sqoop</strong>：将一个关系型数据库中的数据导进到Hadoop的 HDFS中，也可以将HDFS的数据导进到关系型数据库中。</p>
</li>
</ul>

        <h3 id="Hadoop-1"   >
          <a href="#Hadoop-1" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop-1" class="headerlink" title="Hadoop"></a>Hadoop</h3>
      <p>狭义上说，Hadoop指Apache这款开源框架，它的核心组件有：</p>
<ul>
<li><p><strong>HDFS（分布式文件系统）</strong>：解决海量数据存储</p>
</li>
<li><p><strong>YARN（作业调度和集群资源管理的框架）</strong>：解决资源任务调度</p>
</li>
<li><p><strong>MAPREDUCE（分布式运算编程框架）</strong>：解决海量数据计算</p>
</li>
</ul>

        <h2 id="Hadoop的特性优点"   >
          <a href="#Hadoop的特性优点" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop的特性优点" class="headerlink" title="Hadoop的特性优点"></a>Hadoop的特性优点</h2>
      <ul>
<li>扩容能力（Scalable）：Hadoop是在可用的计算机集群间分配数据并完成计算任务的，这些集群可用方便的扩展到数以千计的节点中。</li>
<li>成本低（Economical）：Hadoop通过普通廉价的机器组成服务器集群来分发以及处理数据，以至于成本很低。</li>
<li>高效率（Efficient）：通过并发数据，Hadoop可以在节点之间动态并行的移动数据，使得速度非常快。</li>
<li>可靠性（Rellable）：能自动维护数据的多份复制，并且在任务失败后能自动地重新部署（redeploy）计算任务。所以Hadoop的按位存储和处理数据的能力值得人们信赖。</li>
</ul>

        <h2 id="Hadoop的运行模式"   >
          <a href="#Hadoop的运行模式" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop的运行模式" class="headerlink" title="Hadoop的运行模式"></a>Hadoop的运行模式</h2>
      <p>单机版、伪分布式模式、完全分布式模式</p>

        <h2 id="Hadoop集群启动节点"   >
          <a href="#Hadoop集群启动节点" class="heading-link"><i class="fas fa-link"></i></a><a href="#Hadoop集群启动节点" class="headerlink" title="Hadoop集群启动节点"></a>Hadoop集群启动节点</h2>
      <ul>
<li><strong>namenode</strong>：HDFS的守护进程，负责维护整个文件系统，存储着整个文件系统的元数据信息，image+edit log </li>
<li><strong>datanode</strong>：是具体文件系统的工作节点，当我们需要某个数据，namenode告诉我们去哪里找，就直接和那个DataNode对应的服务器的后台进程进行通信，由DataNode进行数据的检索，然后进行具体的读&#x2F;写操作</li>
<li><strong>secondarynamenode</strong>：它不是namenode的冗余守护进程，而是提供周期检查点和清理任务。帮助NN合并image和editslog，减少NN启动时间。</li>
<li><strong>resourcemanager</strong>：是yarn平台的守护进程，负责所有资源的分配与调度，client的请求由此负责，监控nodemanager</li>
<li><strong>nodemanager</strong>：是单个节点的资源管理，执行来自resourcemanager的具体任务和命令</li>
<li><strong>DFSZKFailoverController</strong>：高可用时它负责监控NN的状态，并及时的把状态信息写入ZK。它通过一个独立线程周期性的调用NN上的一个特定接口来获取NN的健康状态。FC也有选择谁作为Active NN的权利，因为最多只有两个节点，目前选择策略还比较简单（先到先得，轮换）。</li>
<li><strong>JournalNode</strong>：高可用情况下存放namenode的editlog文件</li>
</ul>

        <h2 id="主要配置文件"   >
          <a href="#主要配置文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#主要配置文件" class="headerlink" title="主要配置文件"></a>主要配置文件</h2>
      <ul>
<li><p>hadoop-env.sh</p>
<ul>
<li>文件中设置的是Hadoop运行时需要的环境变量。JAVA_HOME是必须设置的，即使我们当前的系统中设置了JAVA_HOME，它也是不认识的，因为Hadoop即使是在本机上执行，它也是把当前的执行环境当成远程服务器。</li>
</ul>
</li>
<li><p>core-site.xml</p>
<ul>
<li><p>设置Hadoop的文件系统地址</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">name</span>&gt;</span>fs.defaultFS<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">value</span>&gt;</span>hdfs://node-1:9000<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
<li><p>hdfs-site.xml</p>
<ul>
<li><p>指定HDFS副本的数量</p>
</li>
<li><p>secondary namenode 所在主机的ip和端口</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.replication<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">value</span>&gt;</span>2<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line"> 		 <span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.namenode.secondary.http-address<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">  		 <span class="tag">&lt;<span class="name">value</span>&gt;</span>node-2:50090<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
<li><p>mapred-site.xml</p>
<ul>
<li><p>指定mr运行时框架，这里指定在yarn上，默认是local</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">name</span>&gt;</span>mapreduce.framework.name<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">value</span>&gt;</span>yarn<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
<li><p>yarn-site.xml</p>
<ul>
<li><p>指定YARN的主角色（ResourceManager）的地址</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">name</span>&gt;</span>yarn.resourcemanager.hostname<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">value</span>&gt;</span>node-1<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
</ul>

        <h2 id="重要命令"   >
          <a href="#重要命令" class="heading-link"><i class="fas fa-link"></i></a><a href="#重要命令" class="headerlink" title="重要命令"></a>重要命令</h2>
      <figure class="highlight scala"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//初始化</span></span><br><span class="line">hadoop namenode –format</span><br><span class="line"></span><br><span class="line"><span class="comment">//启动dfs</span></span><br><span class="line">start-dfs.sh</span><br><span class="line"></span><br><span class="line"><span class="comment">//启动yarn</span></span><br><span class="line">start-yarn.sh</span><br><span class="line"></span><br><span class="line"><span class="comment">//启动任务历史服务器</span></span><br><span class="line">mr-jobhistory-daemon.sh start historyserver</span><br><span class="line"></span><br><span class="line"><span class="comment">//一键启动</span></span><br><span class="line">start-all.sh</span><br></pre></td></tr></table></div></figure>

<p>启动成功后：</p>
<p>NameNode的web 访问端口:50070.</p>
<p>ResourceManager的web 访问端口:8088</p>
<p>历史服务器 的web 访问端口：19888</p>
<div class="table-container"><table>
<thead>
<tr>
<th>选项名称</th>
<th>使用格式</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-ls</td>
<td><code>-ls &lt;路径&gt;</code></td>
<td>查看指定路径的当前目录结构</td>
</tr>
<tr>
<td>-lsr</td>
<td><code>-lsr &lt;路径&gt;</code></td>
<td>递归查看指定路径的目录结构</td>
</tr>
<tr>
<td>-du</td>
<td><code>-du &lt;路径&gt;</code></td>
<td>统计目录下个文件大小</td>
</tr>
<tr>
<td>-dus</td>
<td><code>-dus &lt;路径&gt;</code></td>
<td>汇总统计目录下文件(夹)大小</td>
</tr>
<tr>
<td>-count</td>
<td><code>-count [-q] &lt;路径&gt;</code></td>
<td>统计文件(夹)数量</td>
</tr>
<tr>
<td>-mv</td>
<td><code>-mv &lt;源路径&gt; &lt;目的路径&gt;</code></td>
<td>移动</td>
</tr>
<tr>
<td>-cp</td>
<td><code>-cp &lt;源路径&gt; &lt;目的路径&gt;</code></td>
<td>复制</td>
</tr>
<tr>
<td>-rm</td>
<td><code>-rm [-skipTrash] &lt;路径&gt;</code></td>
<td>删除文件&#x2F;空白文件夹</td>
</tr>
<tr>
<td>-rmr</td>
<td><code>-rmr [-skipTrash] &lt;路径&gt;</code></td>
<td>递归删除</td>
</tr>
<tr>
<td>-put</td>
<td><code>-put &lt;多个linux上的文件&gt; &lt;hdfs路径&gt;</code></td>
<td>上传文件</td>
</tr>
<tr>
<td>-copyFromLocal</td>
<td><code>-copyFromLocal &lt;多个linux上的文件&gt; &lt;hdfs路径&gt;</code></td>
<td>从本地复制</td>
</tr>
<tr>
<td>-moveFromLocal</td>
<td><code>-moveFromLocal &lt;多个linux上的文件&gt; &lt;hdfs路径&gt;</code></td>
<td>从本地移动</td>
</tr>
<tr>
<td>-getmerge</td>
<td><code>-getmerge &lt;源路径&gt; &lt;linux路径&gt;</code></td>
<td>合并到本地</td>
</tr>
<tr>
<td>-cat</td>
<td><code>-cat &lt;hdfs路径&gt;</code></td>
<td>查看文件内容</td>
</tr>
<tr>
<td>-text</td>
<td><code>-text &lt;hdfs路径&gt;</code></td>
<td>查看文件内容</td>
</tr>
<tr>
<td>-copyToLocal</td>
<td><code>-copyToLocal [-ignoreCrc] [-crc] [hdfs源路径] [linux目的路径]</code></td>
<td>从本地复制</td>
</tr>
<tr>
<td>-moveToLocal</td>
<td><code>-moveToLocal [-crc] &lt;hdfs源路径&gt; &lt;linux目的路径&gt;</code></td>
<td>从本地移动</td>
</tr>
<tr>
<td>-mkdir</td>
<td><code>-mkdir &lt;hdfs路径&gt;</code></td>
<td>创建空白文件夹</td>
</tr>
<tr>
<td>-setrep</td>
<td><code>-setrep [-R] [-w] &lt;副本数&gt; &lt;路径&gt;</code></td>
<td>修改副本数量</td>
</tr>
<tr>
<td>-touchz</td>
<td><code>-touchz &lt;文件路径&gt;</code></td>
<td>创建空白文件</td>
</tr>
<tr>
<td>-stat</td>
<td><code>-stat [format] &lt;路径&gt;</code></td>
<td>显示文件统计信息</td>
</tr>
<tr>
<td>-tail</td>
<td><code>-tail [-f] &lt;文件&gt;</code></td>
<td>查看文件尾部信息</td>
</tr>
<tr>
<td>-chmod</td>
<td><code>-chmod [-R] &lt;权限模式&gt; [路径]</code></td>
<td>修改权限</td>
</tr>
<tr>
<td>-chown</td>
<td><code>-chown [-R] [属主][:[属组]] 路径</code></td>
<td>修改属主</td>
</tr>
<tr>
<td>-chgrp</td>
<td><code>-chgrp [-R] 属组名称 路径</code></td>
<td>修改属组</td>
</tr>
<tr>
<td>-help</td>
<td><code>-help [命令选项]</code></td>
<td>帮助</td>
</tr>
</tbody></table></div>

        <h2 id="HDFS"   >
          <a href="#HDFS" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS" class="headerlink" title="HDFS"></a>HDFS</h2>
      
        <h3 id="HDFS的组成架构"   >
          <a href="#HDFS的组成架构" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS的组成架构" class="headerlink" title="HDFS的组成架构"></a>HDFS的组成架构</h3>
      <img src="https://s2.loli.net/2023/03/26/iFqK32fgIEHmSar.png" alt="HDFS架构" style="zoom: 50%;" />

<ul>
<li><strong>Client</strong>：就是客户端。<br>（1）文件切分。文件上传HDFS的时候，Client将文件切分成一个一个的Block，然后进行存储；<br>  （2）与NameNode交互，获取文件的位置信息；<br>  （3）与DataNode交互，读取或者写入数据；<br>  （4）Client提供一些命令来管理HDFS，比如启动或者关闭HDFS；<br>  （5）Client可以通过一些命令来访问HDFS；</li>
<li><strong>NameNode</strong>：就是Master，它是一个主管、管理者。<br>（1）管理HDFS的名称空间；<br>  （2）管理数据块（Block）映射信息；<br>  （3）配置副本策略；<br>  （4）处理客户端读写请求。</li>
<li><strong>DataNode</strong>：就是Slave。NameNode下达命令，DataNode执行实际的操作。<br>（1）存储实际的数据块；<br>  （2）执行数据块的读&#x2F;写操作。</li>
<li><strong>SecondaryNameNode</strong>：并非NameNode的热备。当NameNode挂掉的时候，它并不能马上替换NameNode并提供服务。<br>（1）辅助NameNode，分担其工作量；<br>  （2）定期合并Fsimage和Edits，并推送给NameNode；<br>  （3）在紧急情况下，可辅助恢复NameNode。</li>
</ul>

        <h3 id="HDFS写数据流程"   >
          <a href="#HDFS写数据流程" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS写数据流程" class="headerlink" title="HDFS写数据流程"></a>HDFS写数据流程</h3>
      <p>HDFS dfs -put a.txt &#x2F;</p>
<p><img src="https://s2.loli.net/2023/03/26/4n2sXymKdZGYDwu.png" alt="HDFS写数据流程"></p>
<p>详细步骤：</p>
<ol>
<li>客户端通过Distributed FileSystem模块向namenode请求上传文件，namenode检查目标文件是否已存在，父目录是否存在。</li>
<li>namenode返回是否可以上传。</li>
<li>客户端请求第一个 block上传到哪几个datanode服务器上。</li>
<li>namenode返回3个datanode节点，分别为dn1、dn2、dn3。</li>
<li>客户端通过FSDataOutputStream模块请求dn1上传数据，dn1收到请求会继续调用dn2，然后dn2调用dn3，将这个通信管道建立完成。</li>
<li>dn1、dn2、dn3逐级应答客户端。</li>
<li>客户端开始往dn1上传第一个block（先从磁盘读取数据放到一个本地内存缓存），以packet为单位（大小为64k），dn1收到一个packet就会传给dn2，dn2传给dn3；dn1每传一个packet会放入一个应答队列等待应答。</li>
<li>当一个block传输完成之后，客户端再次请求namenode上传第二个block的服务器。（重复执行3-7步）。</li>
</ol>

        <h3 id="HDFS读数据流程"   >
          <a href="#HDFS读数据流程" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS读数据流程" class="headerlink" title="HDFS读数据流程"></a>HDFS读数据流程</h3>
      <p><img src="https://s2.loli.net/2023/03/26/br8DXOlMwAcspPV.png" alt="HDFS读数据流程"></p>
<p><strong>详细步骤</strong>：</p>
<ol>
<li>客户端通过Distributed FileSystem向namenode请求下载文件，namenode通过查询元数据，找到文件块所在的datanode地址。</li>
<li>挑选一台datanode（就近原则，然后随机）服务器，请求读取数据。</li>
<li>datanode开始传输数据给客户端（从磁盘里面读取数据输入流，以packet为单位来做校验,大小为64k）。</li>
<li>客户端以packet为单位接收，先在本地缓存，然后写入目标文件。</li>
</ol>

        <h3 id="SecondaryNameNode的作用"   >
          <a href="#SecondaryNameNode的作用" class="heading-link"><i class="fas fa-link"></i></a><a href="#SecondaryNameNode的作用" class="headerlink" title="SecondaryNameNode的作用"></a>SecondaryNameNode的作用</h3>
      <p>合并NameNode的<strong>editslog</strong>和<strong>fsimage</strong></p>
<img src="https://s2.loli.net/2023/03/26/LoXsONqRiDtWJcp.png" alt="SecondaryNameNode的作用" style="zoom:50%;" />




        <h3 id="NameNode与SecondaryNameNode"   >
          <a href="#NameNode与SecondaryNameNode" class="heading-link"><i class="fas fa-link"></i></a><a href="#NameNode与SecondaryNameNode" class="headerlink" title="NameNode与SecondaryNameNode"></a>NameNode与SecondaryNameNode</h3>
      
        <h4 id="区别"   >
          <a href="#区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#区别" class="headerlink" title="区别"></a>区别</h4>
      <p>  （1）NameNode负责管理整个文件系统的元数据，以及每一个路径（文件）所对应的数据块信息。<br>  （2）SecondaryNameNode主要用于定期合并命名空间镜像和命名空间镜像的编辑日志。</p>

        <h4 id="联系"   >
          <a href="#联系" class="heading-link"><i class="fas fa-link"></i></a><a href="#联系" class="headerlink" title="联系"></a>联系</h4>
      <p>  （1）SecondaryNameNode中保存了一份和namenode一致的镜像文件（fsimage）和编辑日志（edits）。<br>  （2）在主namenode发生故障时（假设没有及时备份数据），可以从SecondaryNameNode恢复数据。</p>

        <h3 id="HDFS的垃圾桶机制"   >
          <a href="#HDFS的垃圾桶机制" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS的垃圾桶机制" class="headerlink" title="HDFS的垃圾桶机制"></a>HDFS的垃圾桶机制</h3>
      <p>修改core-site.xml</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">name</span>&gt;</span>fs.trash.interval<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">value</span>&gt;</span>1440<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p>注：这个时间以分钟为单位，例如1440&#x3D;24h&#x3D;1天。HDFS的垃圾回收的默认配置属性为 0，也就是说，如果你不小心误删除了某样东西，那么这个操作是不可恢复的。 </p>

        <h3 id="HANameNode工作原理"   >
          <a href="#HANameNode工作原理" class="heading-link"><i class="fas fa-link"></i></a><a href="#HANameNode工作原理" class="headerlink" title="HANameNode工作原理"></a>HANameNode工作原理</h3>
      <p><img src="https://s2.loli.net/2023/03/26/bSkpPK1ZcRW9sF4.png" alt="HANameNode的工作原理"></p>
<p>ZKFailoverController主要职责</p>
<ol>
<li>健康监测：周期性的向它监控的NN发送健康探测命令，从而来确定某个NameNode是否处于健康状态，如果机器宕机，心跳失败，那么zkfc就会标记它处于一个不健康的状态。</li>
<li>会话管理：如果NN是健康的，zkfc就会在zookeeper中保持一个打开的会话，如果NameNode同时还是Active状态的，那么zkfc还会在Zookeeper中占有一个类型为短暂类型的znode，当这个NN挂掉时，这个znode将会被删除，然后备用的NN，将会得到这把锁，升级为主NN，同时标记状态为Active。</li>
<li>当宕机的NN新启动时，它会再次注册zookeper，发现已经有znode锁了，便会自动变为Standby状态，如此往复循环，保证高可靠，需要注意，目前仅仅支持最多配置2个NN。</li>
<li>master选举：如上所述，通过在zookeeper中维持一个短暂类型的znode，来实现抢占式的锁机制，从而判断那个NameNode为Active状态</li>
</ol>
<p>注：同时出现两个Active状态Onamenode的术语叫脑裂brain split。</p>
<p>防止脑裂的两种方式：</p>
<ol>
<li>ssh发送kill指令</li>
<li>调用用户自定义脚本程序</li>
</ol>

        <h3 id="HDFS中block"   >
          <a href="#HDFS中block" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS中block" class="headerlink" title="HDFS中block"></a>HDFS中block</h3>
      <p>默认保存3份</p>
<p>老版本默认64m，2.x版本默认128m</p>

        <h3 id="HDFS安全模式"   >
          <a href="#HDFS安全模式" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS安全模式" class="headerlink" title="HDFS安全模式"></a>HDFS安全模式</h3>
      <p>文件系统<strong>只接受读</strong>数据请求，而不接受删除、修改等变更请求。</p>
<p>在NameNode主节点启动时，HDFS首先进入安全模式，集群会开始检查数据块的完整性。DataNode在启动的时候会向namenode汇报可用的block信息，当整个系统达到安全标准时，HDFS自动离开安全模式。</p>
<ul>
<li><p>手动进入安全模式</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfsadmin -safemode enter</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>手动离开安全模式</p>
<figure class="highlight sh"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfsadmin -safemode leave</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="机架感知"   >
          <a href="#机架感知" class="heading-link"><i class="fas fa-link"></i></a><a href="#机架感知" class="headerlink" title="机架感知"></a>机架感知</h3>
      <p>hadoop自身是没有机架感知能力的，必须通过人为的设定来达到这个目的。</p>
<p>通过配置一个脚本来进行映射；</p>
<p>通过实现DNSToSwitchMapping接口的resolve()方法来完成网络位置的映射。</p>
<p>1、写一个脚本，然后放到hadoop的core-site.xml配置文件中，用namenode和jobtracker进行调用。</p>
<figure class="highlight python"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/python</span></span><br><span class="line"><span class="comment">#-*-coding:UTF-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line">rack = &#123;<span class="string">&quot;hadoop-node-31&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-32&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-33&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-34&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-49&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-50&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-51&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-52&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-53&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;hadoop-node-54&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.31&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.32&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.33&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.34&quot;</span>:<span class="string">&quot;rack1&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.49&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.50&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.51&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.52&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.53&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                <span class="string">&quot;192.168.1.54&quot;</span>:<span class="string">&quot;rack2&quot;</span>,</span><br><span class="line">                                &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">        <span class="built_in">print</span> <span class="string">&quot;/&quot;</span> + rack.get(sys.argv[<span class="number">1</span>],<span class="string">&quot;rack0&quot;</span>)</span><br></pre></td></tr></table></div></figure>

<p>2、将脚本赋予可执行权限chmod +x RackAware.py，并放到bin&#x2F;目录下。</p>
<p>3、然后打开conf&#x2F;core-site.html</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">    <span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">name</span>&gt;</span>topology.script.file.name<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">value</span>&gt;</span>/opt/modules/hadoop/hadoop-1.0.3/bin/RackAware.py<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--机架感知脚本路径--&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">name</span>&gt;</span>topology.script.number.args<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">value</span>&gt;</span>20<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--机架服务器数量，由于我写了20个，所以这里写20--&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<p>4、重启Hadoop集群</p>

        <h3 id="HDFS的扩容、缩容"   >
          <a href="#HDFS的扩容、缩容" class="heading-link"><i class="fas fa-link"></i></a><a href="#HDFS的扩容、缩容" class="headerlink" title="HDFS的扩容、缩容"></a>HDFS的扩容、缩容</h3>
      
        <h4 id="动态扩容"   >
          <a href="#动态扩容" class="heading-link"><i class="fas fa-link"></i></a><a href="#动态扩容" class="headerlink" title="动态扩容"></a>动态扩容</h4>
      
        <h5 id="准备"   >
          <a href="#准备" class="heading-link"><i class="fas fa-link"></i></a><a href="#准备" class="headerlink" title="准备"></a>准备</h5>
      <p>修改新机器系统hostname（通过&#x2F;etc&#x2F;sysconfig&#x2F;network进行修改）</p>
<p>修改hosts文件，将集群所有节点hosts配置进去（集群所有节点保持hosts文件统一）</p>
<p>设置NameNode到DataNode的免密码登录（ssh-copy-id命令实现）</p>
<p>修改主节点slaves文件，添加新增节点的ip信息（集群重启时配合一键启动脚本使用）</p>
<p>在新的机器上上传解压一个新的hadoop安装包，从主节点机器上将hadoop的所有配置文件，scp到新的节点上。</p>

        <h5 id="添加datanode"   >
          <a href="#添加datanode" class="heading-link"><i class="fas fa-link"></i></a><a href="#添加datanode" class="headerlink" title="添加datanode"></a>添加datanode</h5>
      <ol>
<li><p>在namenode所在的机器的&#x2F;export&#x2F;servers&#x2F;hadoop-2.6.0-cdh5.14.0&#x2F;etc&#x2F;hadoop目录下创建dfs.hosts文件</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">cd /export/servers/hadoop-2.6.0-cdh5.14.0/etc/hadoop</span><br><span class="line"></span><br><span class="line">vim dfs.hosts</span><br><span class="line"></span><br><span class="line">添加如下主机名称（包含新服役的节点）</span><br><span class="line"></span><br><span class="line">node-1</span><br><span class="line">node-2</span><br><span class="line">node-3</span><br><span class="line">node-4</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>
</li>
<li><p>在namenode机器的hdfs-site.xml配置文件中增加dfs.hosts属性</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /export/servers/hadoop-2.6.0-cdh5.14.0/etc/hadoop</span><br><span class="line"></span><br><span class="line">vim hdfs-site.xml</span><br></pre></td></tr></table></div></figure>

<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.hosts<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">value</span>&gt;</span>/export/servers/hadoop-2.6.0-cdh5.14.0/etc/hadoop/dfs.hosts<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<blockquote>
<p>dfs.hosts属性的意义：命名一个文件，其中包含允许连接到namenode的主机列表。必须指定文件的完整路径名。如果该值为空，则允许所有主机。相当于一个白名单，也可以不配置。</p>
</blockquote>
</li>
<li><p>在新的机器上单独启动datanode</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hadoop-daemon.sh start datanode</span><br></pre></td></tr></table></div></figure>

<p>刷新页面就可以看到新的节点加入进来了</p>
</li>
</ol>

        <h5 id="datanode负载均衡服务"   >
          <a href="#datanode负载均衡服务" class="heading-link"><i class="fas fa-link"></i></a><a href="#datanode负载均衡服务" class="headerlink" title="datanode负载均衡服务"></a>datanode负载均衡服务</h5>
      <p>新加入的节点，没有数据块的存储，使得集群整体来看负载还不均衡。因此最后还需要对hdfs负载设置均衡，因为默认的数据传输带宽比较低，可以设置为64M，即</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfsadmin -setBalancerBandwidth 67108864</span><br></pre></td></tr></table></div></figure>

<p>默认balancer的threshold为10%，即各个节点与集群总的存储使用率相差不超过10%，我们可将其设置为5%。然后启动Balancer，</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sbin/start-balancer.sh -threshold 5</span><br></pre></td></tr></table></div></figure>

<p>等待集群自均衡完成即可。</p>

        <h5 id="添加nodemanager"   >
          <a href="#添加nodemanager" class="heading-link"><i class="fas fa-link"></i></a><a href="#添加nodemanager" class="headerlink" title="添加nodemanager"></a>添加nodemanager</h5>
      <p>在新的机器上单独启动nodemanager：</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn-daemon.sh start nodemanager</span><br></pre></td></tr></table></div></figure>

<p>在web页面确认是否成功启用</p>
<p>在ResourceManager，通过<code>yarn node -list</code>查看集群情况</p>

        <h4 id="动态缩容"   >
          <a href="#动态缩容" class="heading-link"><i class="fas fa-link"></i></a><a href="#动态缩容" class="headerlink" title="动态缩容"></a>动态缩容</h4>
      
        <h5 id="添加退役节点"   >
          <a href="#添加退役节点" class="heading-link"><i class="fas fa-link"></i></a><a href="#添加退役节点" class="headerlink" title="添加退役节点"></a>添加退役节点</h5>
      <p>在namenode所在服务器的hadoop配置目录etc&#x2F;hadoop下创建dfs.hosts.exclude文件，并添加需要退役的主机名称。</p>
<p>注意：该文件当中一定要写真正的主机名或者ip地址都行，不能写node-4</p>
<p><strong>node04.hadoop.com</strong></p>
<p>在namenode机器的hdfs-site.xml配置文件中增加dfs.hosts.exclude属性</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /export/servers/hadoop-2.6.0-cdh5.14.0/etc/hadoop</span><br><span class="line"></span><br><span class="line">vim hdfs-site.xml</span><br></pre></td></tr></table></div></figure>

<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span> </span><br><span class="line">        <span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.hosts.exclude<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">value</span>&gt;</span>/export/servers/hadoop-2.6.0-cdh5.14.0/etc/hadoop/dfs.hosts.exclude<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

<blockquote>
<p>dfs.hosts.exclude属性的意义：命名一个文件，其中包含不允许连接到namenode的主机列表。必须指定文件的完整路径名。如果值为空，则不排除任何主机。</p>
</blockquote>

        <h5 id="刷新集群"   >
          <a href="#刷新集群" class="heading-link"><i class="fas fa-link"></i></a><a href="#刷新集群" class="headerlink" title="刷新集群"></a>刷新集群</h5>
      <p>在namenode所在的机器执行以下命令，刷新namenode，刷新resourceManager。</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfsadmin -refreshNodes</span><br><span class="line"></span><br><span class="line">yarn rmadmin –refreshNodes</span><br></pre></td></tr></table></div></figure>

<p>等待退役节点状态为decommissioned（所有块已经复制完成），停止该节点及节点资源管理器。注意：如果副本数是3，服役的节点小于等于3，是不能退役成功的，需要修改副本数后才能退役。</p>
<p>node-4执行以下命令，停止该节点进程</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /export/servers/hadoop-2.6.0-cdh5.14.0</span><br><span class="line"></span><br><span class="line">sbin/hadoop-daemon.sh stop datanode</span><br><span class="line"></span><br><span class="line">sbin/yarn-daemon.sh stop nodemanager</span><br></pre></td></tr></table></div></figure>

<p>namenode所在节点执行以下命令刷新namenode和resourceManager</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">hdfs dfsadmin –refreshNodes</span><br><span class="line"></span><br><span class="line">yarn rmadmin –refreshNodes</span><br></pre></td></tr></table></div></figure>

<p>namenode所在节点执行以下命令进行均衡负载</p>
<figure class="highlight shell"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd /export/servers/hadoop-2.6.0-cdh5.14.0/</span><br><span class="line"></span><br><span class="line">sbin/start-balancer.sh</span><br></pre></td></tr></table></div></figure>




        <h2 id="MapReduce"   >
          <a href="#MapReduce" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce" class="headerlink" title="MapReduce"></a>MapReduce</h2>
      
        <h3 id="工作流程"   >
          <a href="#工作流程" class="heading-link"><i class="fas fa-link"></i></a><a href="#工作流程" class="headerlink" title="工作流程"></a>工作流程</h3>
      <p><img src="https://s2.loli.net/2023/03/27/PsJhdf4z8QbwHBx.png" alt="MapReduce执行总流程概览"></p>

        <h4 id="分片、格式化"   >
          <a href="#分片、格式化" class="heading-link"><i class="fas fa-link"></i></a><a href="#分片、格式化" class="headerlink" title="分片、格式化"></a>分片、格式化</h4>
      <p>输入 Map 阶段的数据源，必须经过分片和格式化操作。</p>
<p><strong>分片</strong>：指的是将源文件划分为大小相等的小数据块( Hadoop 2.x 中默认 128MB )，也就是分片( split )，Hadoop 会为每一个分片构建一个 Map 任务，并由该任务运行自定义的 <code>map()</code> 函数，从而处理分片里的每一条记录;<br><strong>格式化</strong>：将划分好的分片( split )格式化为键值对<code>&lt;key,value&gt;</code>形式的数据，其中， key 代表偏移量， value 代表每一行内容。</p>

        <h4 id="执行MapTask"   >
          <a href="#执行MapTask" class="heading-link"><i class="fas fa-link"></i></a><a href="#执行MapTask" class="headerlink" title="执行MapTask"></a>执行MapTask</h4>
      <p><img src="https://s2.loli.net/2023/03/27/mSRpNyukFOwLD1n.png" alt="MapTask工作机制"></p>
<ol>
<li><strong>Read</strong> 阶段： MapTask 通过用户编写的 RecordReader ，从输入的 InputSplit 中解析出一个个 key &#x2F; value 。</li>
<li><strong>Map</strong> 阶段：将解析出的 key &#x2F; value 交给用户编写的 Map ()函数处理，并产生一系列新的 key &#x2F; value 。</li>
<li><strong>Collect</strong> 阶段：在用户编写的 map() 函数中，数据处理完成后，一般会调用 outputCollector.collect() 输出结果，在该函数内部，它会将生成的 key &#x2F; value 分片(通过调用 partitioner )，并写入一个环形内存缓冲区中(该缓冲区默认大小是 100MB )。</li>
<li><strong>Spill</strong> 阶段：即“溢写”，当缓冲区快要溢出时(默认达到缓冲区大小的 80 %)，会在本地文件系统创建一个溢出文件，将该缓冲区的数据写入这个文件。</li>
</ol>
<blockquote>
<p>将数据写入本地磁盘前，先要对数据进行一次本地排序，并在必要时对数据进行合并、压缩等操作。</p>
<p>写入磁盘之前，线程会根据 ReduceTask 的数量，将数据分区，一个 Reduce 任务对应一个分区的数据。</p>
<p>这样做的目的是为了避免有些 Reduce 任务分配到大量数据，而有些 Reduce 任务分到很少的数据，甚至没有分到数据的尴尬局面。</p>
<p>如果此时设置了 Combiner ，将排序后的结果进行 Combine 操作，这样做的目的是尽可能少地执行数据写入磁盘的操作。</p>
</blockquote>
<ol start="5">
<li><strong>Combine</strong> 阶段：当所有数据处理完成以后， MapTask 会对所有临时文件进行一次合并，以确保最终只会生成一个数据文件</li>
</ol>
<blockquote>
<p>合并的过程中会不断地进行排序和 Combine 操作，其目的有两个：一是尽量减少每次写人磁盘的数据量;二是尽量减少下一复制阶段网络传输的数据量。最后合并成了一个已分区且已排序的文件。</p>
</blockquote>

        <h4 id="执行shuffle"   >
          <a href="#执行shuffle" class="heading-link"><i class="fas fa-link"></i></a><a href="#执行shuffle" class="headerlink" title="执行shuffle"></a>执行shuffle</h4>
      <p><img src="https://s2.loli.net/2023/03/27/69alzX4u5Mn1cBL.png" alt="1582129765528"></p>
<ul>
<li><p>每一个Mapper进程都有一个环形的内存缓冲区，用来存储Map的输出数据，这个内存缓冲区的默认大小是100MB，当数据达到阙值0.8，也就是80MB的时候，一个后台的程序就会把数据溢写到磁盘中。在将数据溢写到磁盘的过程中要经过复杂的过程，首先要将数据进行分区排序（按照分区号如0，1,2），分区完以后为了避免Map输出数据的内存溢出，可以将Map的输出数据分为各个小文件再进行分区，这样map的输出数据就会被分为了具有多个小文件的分区已排序过的数据。然后将各个小文件分区数据进行合并成为一个大的文件（将各个小文件中分区号相同的进行合并）。</p>
</li>
<li><p>这个时候Reducer启动了三个分别为0,1,2。0号Reducer会取得0号分区 的数据；1号Reducer会取得1号分区的数据；2号Reducer会取得2号分区的数据。</p>
</li>
</ul>

        <h4 id="执行ReduceTask"   >
          <a href="#执行ReduceTask" class="heading-link"><i class="fas fa-link"></i></a><a href="#执行ReduceTask" class="headerlink" title="执行ReduceTask"></a>执行ReduceTask</h4>
      <p><img src="https://s2.loli.net/2023/03/27/XWenGVJ2RpaMKzN.png" alt="ReduceTask工作机制"></p>
<ol>
<li><strong>Copy</strong> 阶段： Reduce 会从各个 MapTask 上远程复制一片数据（每个 MapTask 传来的数据都是有序的），并针对某一片数据，如果其大小超过一定國值，则写到磁盘上，否则直接放到内存中</li>
<li><strong>Merge</strong> 阶段：在远程复制数据的同时， ReduceTask 会启动两个后台线程，分别对内存和磁盘上的文件进行合并，以防止内存使用过多或者磁盘文件过多。</li>
<li><strong>Sort</strong> 阶段：用户编写 reduce() 方法输入数据是按 key 进行聚集的一组数据。</li>
</ol>
<blockquote>
<p>为了将 key 相同的数据聚在一起， Hadoop 采用了基于排序的策略。由于各个 MapTask 已经实现对自己的处理结果进行了局部排序，因此， ReduceTask 只需对所有数据进行一次归并排序即可。</p>
</blockquote>
<ol start="4">
<li><strong>Reduce</strong> 阶段：对排序后的键值对调用 reduce() 方法，键相等的键值对调用一次 reduce()方法，每次调用会产生零个或者多个键值对，最后把这些输出的键值对写入到 HDFS 中</li>
<li><strong>Write</strong> 阶段： reduce() 函数将计算结果写到 HDFS 上。</li>
</ol>
<blockquote>
<p>合并的过程中会产生许多的中间文件(写入磁盘了)，但 MapReduce 会让写入磁盘的数据尽可能地少，并且最后一次合并的结果并没有写入磁盘，而是直接输入到 Reduce 函数。</p>
</blockquote>

        <h3 id="combiner"   >
          <a href="#combiner" class="heading-link"><i class="fas fa-link"></i></a><a href="#combiner" class="headerlink" title="combiner"></a>combiner</h3>
      
        <h4 id="流程"   >
          <a href="#流程" class="heading-link"><i class="fas fa-link"></i></a><a href="#流程" class="headerlink" title="流程"></a>流程</h4>
      <img src="https://s2.loli.net/2023/03/27/RvnjuKhrNe4Fp6O.png" alt="combiner" style="zoom: 80%;" />

<ol>
<li><p>Combiner的意义就是对每一个maptask的输出进行局部汇总，以减小网络传输量。</p>
</li>
<li><p>Combiner能够应用的前提是不能影响最终的业务逻辑，而且，Combiner的输出kv应该跟reducer的输入kv类型要对应起来。</p>
</li>
<li><p>Combiner和reducer的区别在于运行的位置:</p>
<p>Combiner是在每一个maptask所在的节点运行;</p>
<p>​	 Reducer是接收全局所有Mapper的输出结果。</p>
</li>
</ol>

        <h4 id="代码"   >
          <a href="#代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#代码" class="headerlink" title="代码"></a>代码</h4>
      <p>自定义Combiner：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">MyCombiner</span> <span class="keyword">extends</span>  <span class="title class_">Reducer</span>&lt;Text, LongWritable, Text, LongWritable&gt; &#123;</span><br><span class="line">        <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">reduce</span><span class="params">(</span></span><br><span class="line"><span class="params">                Text key, Iterable&lt;LongWritable&gt; values,Context context)</span><span class="keyword">throws</span> IOException, InterruptedException &#123;</span><br><span class="line"></span><br><span class="line">            <span class="type">long</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0L</span>;</span><br><span class="line">            <span class="keyword">for</span> (LongWritable value : values) &#123;</span><br><span class="line">                count += value.get();</span><br><span class="line">            &#125;</span><br><span class="line">            context.write(key, <span class="keyword">new</span> <span class="title class_">LongWritable</span>(count));</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>

<p>在主类中添加：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Combiner设置</span><br><span class="line">    <span class="comment">// 设置Map规约Combiner</span></span><br><span class="line">    job.setCombinerClass(MyCombiner.class);</span><br></pre></td></tr></table></div></figure>




        <h3 id="partitioner"   >
          <a href="#partitioner" class="heading-link"><i class="fas fa-link"></i></a><a href="#partitioner" class="headerlink" title="partitioner"></a>partitioner</h3>
      <p>在进行MapReduce计算时，有时候需要把最终的输出数据分到不同的文件中，比如按照省份划分的话，需要把同一省份的数据放到一个文件中；按照性别划分的话，需要把同一性别的数据放到一个文件中。负责实现划分数据的类称作Partitioner。</p>

        <h4 id="HashPartitioner（默认）"   >
          <a href="#HashPartitioner（默认）" class="heading-link"><i class="fas fa-link"></i></a><a href="#HashPartitioner（默认）" class="headerlink" title="HashPartitioner（默认）"></a>HashPartitioner（默认）</h4>
      <p>源码如下：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.apache.hadoop.mapreduce.lib.partition;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.mapreduce.Partitioner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** Partition keys by their &#123;<span class="doctag">@link</span> Object#hashCode()&#125;. */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashPartitioner</span>&lt;K, V&gt; <span class="keyword">extends</span> <span class="title class_">Partitioner</span>&lt;K, V&gt; &#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/** Use &#123;<span class="doctag">@link</span> Object#hashCode()&#125; to partition. */</span></span><br><span class="line">  <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getPartition</span><span class="params">(K key, V value,</span></span><br><span class="line"><span class="params">                          <span class="type">int</span> numReduceTasks)</span> &#123;</span><br><span class="line">    <span class="comment">//默认使用key的hash值与上int的最大值，避免出现数据溢出 的情况</span></span><br><span class="line">    <span class="keyword">return</span> (key.hashCode() &amp; Integer.MAX_VALUE) % numReduceTasks;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>key、value分别指的是Mapper任务的输出，numReduceTasks指的是设置的Reducer任务数量，默认值是1。那么任何整数与1相除的余数肯定是0。也就是说getPartition(…)方法的返回值总是0。也就是Mapper任务的输出总是送给一个Reducer任务，最终只能输出到一个文件中。</p>

        <h4 id="自定义Partitioner"   >
          <a href="#自定义Partitioner" class="heading-link"><i class="fas fa-link"></i></a><a href="#自定义Partitioner" class="headerlink" title="自定义Partitioner"></a>自定义Partitioner</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.hadoop.io.IntWritable;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.mapreduce.Partitioner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FivePartitioner</span> <span class="keyword">extends</span> <span class="title class_">Partitioner</span>&lt;IntWritable, IntWritable&gt;&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 我们的需求：按照能否被5除尽去分区</span></span><br><span class="line"><span class="comment">     * 1、如果除以5的余数是0，  放在0号分区</span></span><br><span class="line"><span class="comment">     * 2、如果除以5的余数不是0，  放在1分区</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getPartition</span><span class="params">(IntWritable key, IntWritable value, <span class="type">int</span> numPartitions)</span> &#123;</span><br><span class="line">        </span><br><span class="line">        <span class="type">int</span> <span class="variable">intValue</span> <span class="operator">=</span> key.get();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span>(intValue % <span class="number">5</span> == <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">           <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>再在主函数里加入如下两行代码即可：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">job.setPartitionerClass(FivePartitioner.class);</span><br><span class="line">job.setNumReduceTasks(<span class="number">2</span>);<span class="comment">//设置为2</span></span><br></pre></td></tr></table></div></figure>




        <h3 id="序列化和反序列化"   >
          <a href="#序列化和反序列化" class="heading-link"><i class="fas fa-link"></i></a><a href="#序列化和反序列化" class="headerlink" title="序列化和反序列化"></a>序列化和反序列化</h3>
      <p>Java的序列化是一个重量级序列化框架（<code>Serializable</code>），一个对象被序列化后，会附带很多额外的信息（各种校验信息，header，继承体系等），不便于在网络中高效传输。所以，hadoop自己开发了一套序列化机制（<code>Writable</code>），精简、高效。<br>自定义bean对象要想序列化传输步骤及注意事项：<br>  （1）必须实现<code>Writable</code>接口<br>  （2）反序列化时，需要反射调用空参构造函数，所以必须有空参构造<br>  （3）重写序列化方法<br>  （4）重写反序列化方法<br>  （5）注意反序列化的顺序和序列化的顺序完全一致<br>  （6）要想把结果显示在文件中，需要重写<code>toString()</code>，且用”\t”分开，方便后续用<br>  （7）如果需要将自定义的bean放在key中传输，则还需要实现<code>comparable</code>接口，因为mapreduce框中的shuffle过程一定会对key进行排序</p>

        <h3 id="InputSplit"   >
          <a href="#InputSplit" class="heading-link"><i class="fas fa-link"></i></a><a href="#InputSplit" class="headerlink" title="InputSplit"></a>InputSplit</h3>
      <p>FileInputFormat源码解析(<code>input.getSplits(job)</code>)<br>（1）找到你数据存储的目录。<br>（2）开始遍历处理（规划切片）目录下的每一个文件。<br>（3）遍历第一个文件ss.txt。<br>  a）获取文件大小<code>fs.sizeOf(ss.txt)</code>;。<br>  b）计算切片大小</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">computeSliteSize(Math.max(minSize,Math.min(maxSize,blocksize)))=blocksize=128M</span><br></pre></td></tr></table></div></figure>

<p>  c）<strong>默认情况下，切片大小&#x3D;blocksize</strong>。<br>  d）开始切，形成第1个切片：ss.txt—0:128M 第2个切片ss.txt—128:256M 第3个切片ss.txt—256M:300M（每次切片时，都要判断切完剩下的部分是否大于块的1.1倍，<strong>不大于1.1倍就划分一块切片</strong>）。<br>  e）将切片信息写到一个切片规划文件中。<br>  f）整个切片的核心过程在<code>getSplit()</code>方法中完成。<br>  g）数据切片只是在逻辑上对输入数据进行分片，并不会再磁盘上将其切分成分片进行存储。InputSplit只记录了分片的元数据信息，比如起始位置、长度以及所在的节点列表等。<br>  h）注意：block是HDFS上物理上存储的存储的数据，切片是对数据逻辑上的划分。<br>（4）<strong>提交切片规划文件到yarn上，yarn上的MrAppMaster就可以根据切片规划文件计算开启maptask个数</strong>。</p>

        <h3 id="一个job的map和reduce的数"   >
          <a href="#一个job的map和reduce的数" class="heading-link"><i class="fas fa-link"></i></a><a href="#一个job的map和reduce的数" class="headerlink" title="一个job的map和reduce的数"></a>一个job的map和reduce的数</h3>
      
        <h4 id="map数量"   >
          <a href="#map数量" class="heading-link"><i class="fas fa-link"></i></a><a href="#map数量" class="headerlink" title="map数量"></a>map数量</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">splitSize=max&#123;minSize,min&#123;maxSize,blockSize&#125;&#125;</span><br></pre></td></tr></table></div></figure>

<p>由处理的数据分成的block数量决定default_num &#x3D; total_size &#x2F; split_size,即切片个数。</p>

        <h4 id="reduce数量"   >
          <a href="#reduce数量" class="heading-link"><i class="fas fa-link"></i></a><a href="#reduce数量" class="headerlink" title="reduce数量"></a>reduce数量</h4>
      <p><code>job.setNumReduceTasks(x)</code>;x 为reduce的数量。不设置的话默认为 1。</p>

        <h3 id="MapReduce中的排序"   >
          <a href="#MapReduce中的排序" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce中的排序" class="headerlink" title="MapReduce中的排序"></a>MapReduce中的排序</h3>
      <p>部分排序、全排序、辅助排序、二次排序、自定义排序</p>
<p>发生的阶段：<br>map side：发生在spill后partition前。<br>reduce side：发生在copy后 reduce前。</p>

        <h3 id="缓存机制（Distributedcache）"   >
          <a href="#缓存机制（Distributedcache）" class="heading-link"><i class="fas fa-link"></i></a><a href="#缓存机制（Distributedcache）" class="headerlink" title="缓存机制（Distributedcache）"></a>缓存机制（Distributedcache）</h3>
      <p>在进行join操作的时候，如果一个表很大，另一个表很小，我们就可以将这个小表进行广播处理，即每个计算节点上都存一份，然后进行map端的连接操作</p>

        <h3 id="MapReduce无法提速的场景"   >
          <a href="#MapReduce无法提速的场景" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce无法提速的场景" class="headerlink" title="MapReduce无法提速的场景"></a>MapReduce无法提速的场景</h3>
      <p>数据量很小。<br>繁杂的小文件。<br>索引是更好的存取机制的时候。<br>事务处理。<br>只有一台机器的时候。</p>

        <h3 id="实现-TopN"   >
          <a href="#实现-TopN" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现-TopN" class="headerlink" title="实现 TopN"></a>实现 TopN</h3>
      <p>可以自定义groupingcomparator，对结果进行最大值排序，然后再reduce输出时，控制只输出前n个数。就达到了topn输出的目的。</p>

        <h3 id="实现wordcount"   >
          <a href="#实现wordcount" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现wordcount" class="headerlink" title="实现wordcount"></a>实现wordcount</h3>
      <p>定义一个mapper类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先要定义四个泛型的类型</span></span><br><span class="line"><span class="comment">//keyin:  LongWritable    valuein: Text</span></span><br><span class="line"><span class="comment">//keyout: Text            valueout:IntWritable</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">WordCountMapper</span> <span class="keyword">extends</span> <span class="title class_">Mapper</span>&lt;LongWritable, Text, Text, IntWritable&gt;&#123;</span><br><span class="line">	<span class="comment">//map方法的生命周期：  框架每传一行数据就被调用一次</span></span><br><span class="line">	<span class="comment">//key :  这一行的起始点在文件中的偏移量</span></span><br><span class="line">	<span class="comment">//value: 这一行的内容</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">map</span><span class="params">(LongWritable key, Text value, Context context)</span> <span class="keyword">throws</span> IOException, InterruptedException &#123;</span><br><span class="line">		<span class="comment">//拿到一行数据转换为string</span></span><br><span class="line">		<span class="type">String</span> <span class="variable">line</span> <span class="operator">=</span> value.toString();</span><br><span class="line">		<span class="comment">//将这一行切分出各个单词</span></span><br><span class="line">		String[] words = line.split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		<span class="comment">//遍历数组，输出&lt;单词，1&gt;</span></span><br><span class="line">		<span class="keyword">for</span>(String word:words)&#123;</span><br><span class="line">			context.write(<span class="keyword">new</span> <span class="title class_">Text</span>(word), <span class="keyword">new</span> <span class="title class_">IntWritable</span>(<span class="number">1</span>));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>定义一个reducer类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//生命周期：框架每传递进来一个kv 组，reduce方法被调用一次  </span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">reduce</span><span class="params">(Text key, Iterable&lt;IntWritable&gt; values, Context context)</span> <span class="keyword">throws</span> IOException, InterruptedException &#123;</span><br><span class="line">		<span class="comment">//定义一个计数器</span></span><br><span class="line">		<span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//遍历这一组kv的所有v，累加到count中</span></span><br><span class="line">		<span class="keyword">for</span>(IntWritable value:values)&#123;</span><br><span class="line">			count += value.get();</span><br><span class="line">		&#125;</span><br><span class="line">		context.write(key, <span class="keyword">new</span> <span class="title class_">IntWritable</span>(count));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>定义一个主类，用来描述job并提交job</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">WordCountRunner</span> &#123;</span><br><span class="line">	<span class="comment">//把业务逻辑相关的信息（哪个是mapper，哪个是reducer，要处理的数据在哪里，输出的结果放哪里……）描述成一个job对象</span></span><br><span class="line">	<span class="comment">//把这个描述好的job提交给集群去运行</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">		<span class="type">Configuration</span> <span class="variable">conf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Configuration</span>();</span><br><span class="line">		<span class="type">Job</span> <span class="variable">wcjob</span> <span class="operator">=</span> Job.getInstance(conf);</span><br><span class="line">		<span class="comment">//指定我这个job所在的jar包</span></span><br><span class="line"><span class="comment">//		wcjob.setJar(&quot;/home/hadoop/wordcount.jar&quot;);</span></span><br><span class="line">		wcjob.setJarByClass(WordCountRunner.class);</span><br><span class="line">		</span><br><span class="line">		wcjob.setMapperClass(WordCountMapper.class);</span><br><span class="line">		wcjob.setReducerClass(WordCountReducer.class);</span><br><span class="line">		<span class="comment">//设置我们的业务逻辑Mapper类的输出key和value的数据类型</span></span><br><span class="line">		wcjob.setMapOutputKeyClass(Text.class);</span><br><span class="line">		wcjob.setMapOutputValueClass(IntWritable.class);</span><br><span class="line">		<span class="comment">//设置我们的业务逻辑Reducer类的输出key和value的数据类型</span></span><br><span class="line">		wcjob.setOutputKeyClass(Text.class);</span><br><span class="line">		wcjob.setOutputValueClass(IntWritable.class);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//指定要处理的数据所在的位置</span></span><br><span class="line">		FileInputFormat.setInputPaths(wcjob, <span class="string">&quot;hdfs://hdp-server01:9000/wordcount/data/big.txt&quot;</span>);</span><br><span class="line">		<span class="comment">//指定处理完成之后的结果所保存的位置</span></span><br><span class="line">		FileOutputFormat.setOutputPath(wcjob, <span class="keyword">new</span> <span class="title class_">Path</span>(<span class="string">&quot;hdfs://hdp-server01:9000/wordcount/output/&quot;</span>));</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//向yarn集群提交这个job</span></span><br><span class="line">		<span class="type">boolean</span> <span class="variable">res</span> <span class="operator">=</span> wcjob.waitForCompletion(<span class="literal">true</span>);</span><br><span class="line">		System.exit(res?<span class="number">0</span>:<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></div></figure>




        <h3 id="执行MapReduce常见的问题"   >
          <a href="#执行MapReduce常见的问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#执行MapReduce常见的问题" class="headerlink" title="执行MapReduce常见的问题"></a>执行MapReduce常见的问题</h3>
      <ul>
<li>client对集群中HDFS的操作没有权限</li>
</ul>
<p>在集群配置文件hdfs-site.xml，然后重启</p>
<figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">property</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">name</span>&gt;</span>dfs.permissions<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">value</span>&gt;</span>false<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>mapreduce的输出路径已存在，必须先删除掉那个路径</p>
</li>
<li><p>提交集群运行，运行失败</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">job.setJar(<span class="string">&quot;/home/hadoop/wordcount.jar&quot;</span>);</span><br></pre></td></tr></table></div></figure>

<ul>
<li>日志打不出来，报警告信息</li>
</ul>
<figure class="highlight verilog"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">log4j:WARN No appenders could be found <span class="keyword">for</span> logger (org<span class="variable">.apache</span><span class="variable">.hadoop</span><span class="variable">.metrics2</span><span class="variable">.lib</span><span class="variable">.MutableMetricsFactory</span>).  </span><br><span class="line">log4j:WARN Please initialize the log4j system properly.  </span><br><span class="line">log4j:WARN See http:<span class="comment">//logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  </span></span><br></pre></td></tr></table></div></figure>

<p>需要在项目的src下面新建file名为log4j.properties的文件</p>

        <h2 id="yarn"   >
          <a href="#yarn" class="heading-link"><i class="fas fa-link"></i></a><a href="#yarn" class="headerlink" title="yarn"></a>yarn</h2>
      
        <h3 id="yarn三大组件"   >
          <a href="#yarn三大组件" class="heading-link"><i class="fas fa-link"></i></a><a href="#yarn三大组件" class="headerlink" title="yarn三大组件"></a>yarn三大组件</h3>
      <p>ResourceManager负责所有资源的监控、分配和管理；</p>
<p>ApplicationMaster负责每一个具体应用程序的调度和协调；</p>
<p>NodeManager负责每一个节点的维护。</p>

        <h3 id="MapReduce程序在yarn上的执行流程"   >
          <a href="#MapReduce程序在yarn上的执行流程" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce程序在yarn上的执行流程" class="headerlink" title="MapReduce程序在yarn上的执行流程"></a>MapReduce程序在yarn上的执行流程</h3>
      <p>Hadoop jar xxx.jar </p>
<img src="https://s2.loli.net/2023/03/27/Bfo4hTdiAz7FPjc.png" alt="MapReduce程序在yarn上的执行流程" style="zoom:50%;" />

<ol>
<li>客户端向集群提交一个任务，该任务首先到ResourceManager中的ApplicationManager;</li>
<li>ApplicationManager收到任务之后，会在集群中找一个NodeManager，并在该NodeManager所在DataNode上启动一个AppMaster进程，该进程用于进行任务的划分和任务的监控；</li>
<li>AppMaster启动起来之后，会向ResourceManager中的ApplicationManager注册其信息（目的是与之通信）；</li>
<li>AppMaster向ResourceManager下的ResourceScheduler申请计算任务所需的资源；</li>
<li>AppMaster申请到资源之后，会与所有的NodeManager通信要求它们启动计算任务所需的任务（Map和Reduce）；</li>
<li>各个NodeManager启动对应的容器用来执行Map和Reduce任务；</li>
<li>各个任务会向AppMaster汇报自己的执行进度和执行状况，以便让AppMaster随时掌握各个任务的运行状态，在某个任务出了问题之后重启执行该任务；</li>
<li>在任务执行完之后，AppMaster向ApplicationManager汇报，以便让ApplicationManager注销并关闭自己，使得资源得以回收；</li>
</ol>

        <h3 id="调度器（scheduler"   >
          <a href="#调度器（scheduler" class="heading-link"><i class="fas fa-link"></i></a><a href="#调度器（scheduler" class="headerlink" title="调度器（scheduler)"></a>调度器（scheduler)</h3>
      
        <h4 id="FIFO-Scheduler"   >
          <a href="#FIFO-Scheduler" class="heading-link"><i class="fas fa-link"></i></a><a href="#FIFO-Scheduler" class="headerlink" title="FIFO Scheduler"></a>FIFO Scheduler</h4>
      <img src="https://s2.loli.net/2023/03/28/Dqzawrvi4SQkBZA.png" alt="FIFOScheduler" style="zoom:50%;" />

<p>维持一个先入先出队列，按时间顺序执行任务。</p>

        <h4 id="Capacity-Scheduler"   >
          <a href="#Capacity-Scheduler" class="heading-link"><i class="fas fa-link"></i></a><a href="#Capacity-Scheduler" class="headerlink" title="Capacity Scheduler"></a>Capacity Scheduler</h4>
      <img src="https://s2.loli.net/2023/03/28/xjo5U8cCfzFqlYE.png" alt="CapacityScheduler" style="zoom:50%;" />

<p>支持多个队列，每个队列可配置一定的资源量，每个队列采用FIFO调度策略.</p>
<p>（2.7.2版本默认）</p>
<p>为了防止同一个用户的作业独占队列中的资源，该调度器会对同一用户提交</p>
<p>的作业所占资源量进行限定：</p>
<ul>
<li>选择队列：计算每个队列中正在运行的任务数与其应该分得的计算资源之间的比</li>
</ul>
<p>值，选择一个该比值最小的队列——最闲的。</p>
<ul>
<li>选择作业：按照作业优先级和提交时间顺序，同时考虑用户资源量限制和内存限</li>
</ul>
<p>制对队列内任务排序。</p>

        <h4 id="Fair-Scheduler"   >
          <a href="#Fair-Scheduler" class="heading-link"><i class="fas fa-link"></i></a><a href="#Fair-Scheduler" class="headerlink" title="Fair Scheduler"></a>Fair Scheduler</h4>
      <img src="https://s2.loli.net/2023/03/28/OBYQrzbu4hwt251.png" alt="FairScheduler" style="zoom:50%;" />

<p>支持多个队列，每个队列内部按照缺额大小分配资源启动任务，同一时间队列中有多个任务执行。队列的并行度大于等于队列的个数。</p>
<p>缺额：每个job理想情况下获得的计算资源与实际获得的计算资源存在的差距。</p>

        <h3 id="容错性"   >
          <a href="#容错性" class="heading-link"><i class="fas fa-link"></i></a><a href="#容错性" class="headerlink" title="容错性"></a>容错性</h3>
      
        <h4 id="MRAppMaster容错性"   >
          <a href="#MRAppMaster容错性" class="heading-link"><i class="fas fa-link"></i></a><a href="#MRAppMaster容错性" class="headerlink" title="MRAppMaster容错性"></a>MRAppMaster容错性</h4>
      <p>  一旦运行失败，由YARN的ResourceManager负责重新启动，最多重启次数可由用户设置，默认是2次。一旦超过最高重启次数，则作业运行失败。</p>

        <h4 id="Map-Task-x2F-Reduce容错性"   >
          <a href="#Map-Task-x2F-Reduce容错性" class="heading-link"><i class="fas fa-link"></i></a><a href="#Map-Task-x2F-Reduce容错性" class="headerlink" title="Map Task&#x2F;Reduce容错性"></a>Map Task&#x2F;Reduce容错性</h4>
      <p>  Task Task周期性向MRAppMaster汇报心跳；一旦Task挂掉，则MRAppMaster将为之重新申请资源，并运行之。最多重新运行次数可由用户设置，默认4次。</p>

        <h2 id="数据压缩算法"   >
          <a href="#数据压缩算法" class="heading-link"><i class="fas fa-link"></i></a><a href="#数据压缩算法" class="headerlink" title="数据压缩算法"></a>数据压缩算法</h2>
      <p>常用的压缩算法有<strong>bzip2、gzip、lzo、snappy</strong>，其中lzo、snappy需要操作系统安装native库才可以支持。</p>
<p> 一般用Snappy，特点速度快，缺点无法切分（可以回答在链式 MR 中，Reduce 端输出使用 bzip2 压缩，以便后续的 map 任务对数据进行 split）</p>

        <h2 id="优化"   >
          <a href="#优化" class="heading-link"><i class="fas fa-link"></i></a><a href="#优化" class="headerlink" title="优化"></a>优化</h2>
      
        <h3 id="MapReduce跑得慢的原因"   >
          <a href="#MapReduce跑得慢的原因" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce跑得慢的原因" class="headerlink" title="MapReduce跑得慢的原因"></a>MapReduce跑得慢的原因</h3>
      <ul>
<li>计算机性能<br>CPU、内存、磁盘健康、网络</li>
<li>I&#x2F;O 操作优化<br>（1）数据倾斜<br>    （2）map和reduce数设置不合理<br>    （3）reduce等待过久<br>    （4）小文件过多<br>    （5）大量的不可分块的超大文件<br>    （6）spill次数过多<br>    （7）merge次数过多等</li>
</ul>

        <h3 id="MapReduce优化方法"   >
          <a href="#MapReduce优化方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#MapReduce优化方法" class="headerlink" title="MapReduce优化方法"></a>MapReduce优化方法</h3>
      
        <h4 id="数据输入"   >
          <a href="#数据输入" class="heading-link"><i class="fas fa-link"></i></a><a href="#数据输入" class="headerlink" title="数据输入"></a>数据输入</h4>
      <ul>
<li><p>合并小文件：</p>
<p>在执行mr任务前将小文件进行合并，大量的小文件会产生大量的map任务，增大map任务装载次数，而任务的装载比较耗时，从而导致mr运行较慢。</p>
<p>（Hadoop Archive、Sequence file、CombineFileInputFormat）</p>
</li>
<li><p>采用ConbinFileInputFormat来作为输入，解决输入端大量小文件场景。</p>
</li>
</ul>

        <h4 id="map阶段"   >
          <a href="#map阶段" class="heading-link"><i class="fas fa-link"></i></a><a href="#map阶段" class="headerlink" title="map阶段"></a>map阶段</h4>
      <ul>
<li><p>减少spill次数：</p>
<p>通过调整io.sort.mb及sort.spill.percent参数值，增大触发spill的内存上限，减少spill次数，从而减少磁盘 IO。</p>
</li>
<li><p>减少merge次数：</p>
<p>通过调整io.sort.factor参数，增大merge的文件数目，减少merge的次数，从而缩短mr处理时间。</p>
</li>
<li><p>在 map 之后先进行combine处理，减少I&#x2F;O。</p>
</li>
</ul>

        <h4 id="reduce阶段"   >
          <a href="#reduce阶段" class="heading-link"><i class="fas fa-link"></i></a><a href="#reduce阶段" class="headerlink" title="reduce阶段"></a>reduce阶段</h4>
      <ul>
<li><p>合理设置map和reduce数：</p>
<p>两个都不能设置太少，也不能设置太多。太少，会导致task等待，延长处理时间；太多，会导致 map、reduce任务间竞争资源，造成处理超时等错误。</p>
</li>
<li><p>设置map、reduce共存：</p>
<p>调整slowstart.completedmaps参数，使map运行到一定程度后，reduce也开始运行，减少reduce的等待时间。</p>
</li>
<li><p>规避使用reduce：</p>
<p>因为Reduce在用于连接数据集的时候将会产生大量的网络消耗。</p>
</li>
<li><p>合理设置reduce端的buffer：</p>
<p>默认情况下，数据达到一个阈值的时候，buffer中的数据就会写入磁盘，然后reduce会从磁盘中获得所有的数据。也就是说，buffer和reduce是没有直接关联的，中间多个一个写磁盘-&gt;读磁盘的过程，既然有这个弊端，那么就可以通过参数来配置，使得buffer中的一部分数据可以直接输送到reduce，从而减少IO开销：mapred.job.reduce.input.buffer.percent，默认为0.0。当值大于0的时候，会保留指定比例的内存读buffer中的数据直接拿给reduce使用。这样一来，设置buffer需要内存，读取数据需要内存，reduce计算也要内存，所以要根据作业的运行情况进行调整。</p>
</li>
</ul>

        <h4 id="IO传输"   >
          <a href="#IO传输" class="heading-link"><i class="fas fa-link"></i></a><a href="#IO传输" class="headerlink" title="IO传输"></a>IO传输</h4>
      <ul>
<li>采用数据压缩的方式，减少网络IO的时间。安装Snappy和LZOP压缩编码器。</li>
<li>使用SequenceFile二进制文件</li>
</ul>

        <h4 id="数据倾斜问题"   >
          <a href="#数据倾斜问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#数据倾斜问题" class="headerlink" title="数据倾斜问题"></a>数据倾斜问题</h4>
      <ul>
<li>提前在map 进行 combine，减少传输的数据量</li>
</ul>
<p>在 Mapper 加上 combiner 相当于提前进行 reduce，即把一个 Mapper 中的相同 key 进行</p>
<p>了聚合，减少 shuffle 过程中传输的数据量，以及 Reducer 端的计算量。</p>
<p>如果导致数据倾斜的 key 大量分布在不同的 mapper 的时候，这种方法就不是很有效了。</p>
<ul>
<li>导致数据倾斜的key 大量分布在不同的 mapper</li>
</ul>
<p>（1）局部聚合加全局聚合。</p>
<p>第一次在 map 阶段对那些导致了数据倾斜的 key 加上 1 到 n 的随机前缀，这样本来相</p>
<p>同的 key 也会被分到多个 Reducer 中进行局部聚合，数量就会大大降低。</p>
<p>第二次 mapreduce，去掉 key 的随机前缀，进行全局聚合。</p>
<p>思想：二次 mr，第一次将 key 随机散列到不同 reducer 进行处理达到负载均衡目的。第</p>
<p>二次再根据去掉 key 的随机前缀，按原 key 进行 reduce 处理。</p>
<p>这个方法进行两次 mapreduce，性能稍差。</p>
<p>2）增加 Reducer，提升并行度</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JobConf.setNumReduceTasks(<span class="type">int</span>)</span><br></pre></td></tr></table></div></figure>

<p>3）实现自定义分区</p>
<p>根据数据分布情况，自定义散列函数，将 key 均匀分配到不同 Reducer</p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/09/14/5.%E6%B5%81/">5.流</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-09-14</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-09-14</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-Stream流"   >
          <a href="#1-Stream流" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-Stream流" class="headerlink" title="1.Stream流"></a>1.Stream流</h1>
      
        <h2 id="1-1体验Stream流"   >
          <a href="#1-1体验Stream流" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1体验Stream流" class="headerlink" title="1.1体验Stream流"></a>1.1体验Stream流</h2>
      <p>案例需求：按照下面的要求完成集合的创建和遍历</p>
<ul>
<li>创建一个集合，存储多个字符串元素</li>
<li>把集合中所有以”张”开头的元素存储到一个新的集合</li>
<li>把”张”开头的集合中的长度为3的元素存储到一个新的集合</li>
<li>遍历上一步得到的集合</li>
</ul>
<p>原始方式示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把集合中所有以&quot;张&quot;开头的元素存储到一个新的集合</span></span><br><span class="line">        ArrayList&lt;String&gt; zhangList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(String s : list) &#123;</span><br><span class="line">            <span class="keyword">if</span>(s.startsWith(<span class="string">&quot;张&quot;</span>)) &#123;</span><br><span class="line">                zhangList.add(s);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        System.out.println(zhangList);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//把&quot;张&quot;开头的集合中的长度为3的元素存储到一个新的集合</span></span><br><span class="line">        ArrayList&lt;String&gt; threeList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(String s : zhangList) &#123;</span><br><span class="line">            <span class="keyword">if</span>(s.length() == <span class="number">3</span>) &#123;</span><br><span class="line">                threeList.add(s);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        System.out.println(threeList);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历上一步得到的集合</span></span><br><span class="line">        <span class="keyword">for</span>(String s : threeList) &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line">     </span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>使用Stream流示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line">      </span><br><span class="line"><span class="comment">//        list.stream().filter(s -&gt; s.startsWith(&quot;张&quot;)).filter(s -&gt; s.length() == 3).forEach(s -&gt; System.out.println(s));</span></span><br><span class="line">        <span class="comment">//Stream流来改进</span></span><br><span class="line">        list.stream().filter(s -&gt; s.startsWith(<span class="string">&quot;张&quot;</span>)).filter(s -&gt; s.length() == <span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>Stream流的好处：</p>
<ul>
<li>直接阅读代码的字面意思即可完美展示无关逻辑方式的语义：获取流、过滤姓张、过滤长度为3、逐一打印</li>
<li>Stream流把真正的函数式编程风格引入到Java中</li>
<li>代码简洁</li>
</ul>

        <h2 id="1-2-Stream流的常见生成方式"   >
          <a href="#1-2-Stream流的常见生成方式" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-Stream流的常见生成方式" class="headerlink" title="1.2 Stream流的常见生成方式"></a>1.2 Stream流的常见生成方式</h2>
      <p>Stream流的思想</p>
<p><img src="https://s2.loli.net/2022/09/14/tLn4NfuAzwFJjY1.png" alt="Stream流思想"></p>
<p>Stream流的三类方法</p>
<p>1、获取Stream流</p>
<ul>
<li>创建一条流水线,并把数据放到流水线上准备进行操作</li>
</ul>
<p>2、中间方法</p>
<ul>
<li>流水线上的操作</li>
<li>一次操作完毕之后,还可以继续进行其他操作</li>
</ul>
<p>3、终结方法</p>
<ul>
<li>一个Stream流只能有一个终结方法</li>
<li>是流水线上的最后一个操作</li>
</ul>
<p>4、生成Stream流的方式</p>
<ul>
<li><p>Collection体系集合</p>
<p>使用默认方法<code>stream()</code>生成流， <code>default Stream&lt;E&gt; stream()</code></p>
</li>
<li><p>Map体系集合</p>
<p>把Map转成Set集合，间接的生成流</p>
</li>
<li><p>数组</p>
<p>通过Arrays中的静态方法stream生成流</p>
</li>
<li><p>同种数据类型的多个数据</p>
<p>通过Stream接口的静态方法of(T… values)生成流</p>
</li>
</ul>
<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//Collection体系的集合可以使用默认方法stream()生成流</span></span><br><span class="line">        List&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line">        Stream&lt;String&gt; listStream = list.stream();</span><br><span class="line"></span><br><span class="line">        Set&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;String&gt;();</span><br><span class="line">        Stream&lt;String&gt; setStream = set.stream();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Map体系的集合间接的生成流</span></span><br><span class="line">        Map&lt;String,Integer&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String, Integer&gt;();</span><br><span class="line">        Stream&lt;String&gt; keyStream = map.keySet().stream();</span><br><span class="line">        Stream&lt;Integer&gt; valueStream = map.values().stream();</span><br><span class="line">        Stream&lt;Map.Entry&lt;String, Integer&gt;&gt; entryStream = map.entrySet().stream();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//数组可以通过Arrays中的静态方法stream生成流</span></span><br><span class="line">        String[] strArray = &#123;<span class="string">&quot;hello&quot;</span>,<span class="string">&quot;world&quot;</span>,<span class="string">&quot;java&quot;</span>&#125;;</span><br><span class="line">        Stream&lt;String&gt; strArrayStream = Arrays.stream(strArray);</span><br><span class="line">      </span><br><span class="line">      	<span class="comment">//同种数据类型的多个数据可以通过Stream接口的静态方法of(T... values)生成流</span></span><br><span class="line">        Stream&lt;String&gt; strArrayStream2 = Stream.of(<span class="string">&quot;hello&quot;</span>, <span class="string">&quot;world&quot;</span>, <span class="string">&quot;java&quot;</span>);</span><br><span class="line">        Stream&lt;Integer&gt; intStream = Stream.of(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="1-3-Stream流中间操作方法"   >
          <a href="#1-3-Stream流中间操作方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-Stream流中间操作方法" class="headerlink" title="1.3 Stream流中间操作方法"></a>1.3 Stream流中间操作方法</h2>
      <p>概念：中间操作的意思是,执行完此方法之后,Stream流依然可以继续执行其他操作</p>
<p>常见方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cStream&lt;T&gt; <span class="title function_">filter</span><span class="params">(Predicate predicate)</span>		<span class="comment">//用于对流中的数据进行过滤</span></span><br><span class="line">  </span><br><span class="line">Stream&lt;T&gt; <span class="title function_">limit</span><span class="params">(<span class="type">long</span> maxSize)</span>		<span class="comment">//返回此流中的元素组成的流，截取前指定参数个数的数据</span></span><br><span class="line">  </span><br><span class="line">Stream&lt;T&gt; <span class="title function_">skip</span><span class="params">(<span class="type">long</span> n)</span>		<span class="comment">//跳过指定参数个数的数据，返回由该流的剩余元素组成的流</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">static</span> &lt;T&gt; Stream&lt;T&gt; <span class="title function_">concat</span><span class="params">(Stream a, Stream b)</span>		<span class="comment">//合并a和b两个流为一个流</span></span><br><span class="line">  </span><br><span class="line">Stream&lt;T&gt; <span class="title function_">distinct</span><span class="params">()</span>		<span class="comment">//返回由该流的不同元素（根据Object.equals(Object) ）组成的流</span></span><br></pre></td></tr></table></div></figure>

<p>filter代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求1：把list集合中以张开头的元素在控制台输出</span></span><br><span class="line">        list.stream().filter(s -&gt; s.startsWith(<span class="string">&quot;张&quot;</span>)).forEach(System.out::println);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：把list集合中长度为3的元素在控制台输出</span></span><br><span class="line">        list.stream().filter(s -&gt; s.length() == <span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求3：把list集合中以张开头的，长度为3的元素在控制台输出</span></span><br><span class="line">        list.stream().filter(s -&gt; s.startsWith(<span class="string">&quot;张&quot;</span>)).filter(s -&gt; s.length() == <span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>limit&amp;skip代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求1：取前3个数据在控制台输出</span></span><br><span class="line">        list.stream().limit(<span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：跳过3个元素，把剩下的元素在控制台输出</span></span><br><span class="line">        list.stream().skip(<span class="number">3</span>).forEach(System.out::println);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求3：跳过2个元素，把剩下的元素中前2个在控制台输出</span></span><br><span class="line">        list.stream().skip(<span class="number">2</span>).limit(<span class="number">2</span>).forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>concat&amp;distinct代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求1：取前4个数据组成一个流</span></span><br><span class="line">        Stream&lt;String&gt; s1 = list.stream().limit(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：跳过2个数据组成一个流</span></span><br><span class="line">        Stream&lt;String&gt; s2 = list.stream().skip(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求3：合并需求1和需求2得到的流，并把结果在控制台输出</span></span><br><span class="line"><span class="comment">//        Stream.concat(s1,s2).forEach(System.out::println);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求4：合并需求1和需求2得到的流，并把结果在控制台输出，要求字符串元素不能重复</span></span><br><span class="line">        Stream.concat(s1,s2).distinct().forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="1-4-Stream流终结操作方法"   >
          <a href="#1-4-Stream流终结操作方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-Stream流终结操作方法" class="headerlink" title="1.4 Stream流终结操作方法"></a>1.4 Stream流终结操作方法</h2>
      <p>概念：终结操作的意思是,执行完此方法之后,Stream流将不能再执行其他操作</p>
<p>常见方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">forEach</span><span class="params">(Consumer action)</span>		<span class="comment">//对此流的每个元素执行操作</span></span><br><span class="line">  </span><br><span class="line"><span class="type">long</span> <span class="title function_">count</span><span class="params">()</span>		<span class="comment">//返回此流中的元素数</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个集合，存储多个字符串元素</span></span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张敏&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求1：把集合中的元素在控制台输出</span></span><br><span class="line"><span class="comment">//        list.stream().forEach(System.out::println);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：统计集合中有几个以张开头的元素，并把统计结果在控制台输出</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">count</span> <span class="operator">=</span> list.stream().filter(s -&gt; s.startsWith(<span class="string">&quot;张&quot;</span>)).count();</span><br><span class="line">        System.out.println(count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="1-5-Stream流的收集操作"   >
          <a href="#1-5-Stream流的收集操作" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-5-Stream流的收集操作" class="headerlink" title="1.5 Stream流的收集操作"></a>1.5 Stream流的收集操作</h2>
      <p>概念：对数据使用Stream流的方式操作完毕后,可以把流中的数据收集到集合中</p>
<p>常用方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">R <span class="title function_">collect</span><span class="params">(Collector collector)</span>		<span class="comment">//把结果收集到集合中</span></span><br></pre></td></tr></table></div></figure>

<p>工具类Collectors提供了具体的收集方式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; Collector <span class="title function_">toList</span><span class="params">()</span>		<span class="comment">//把元素收集到List集合中</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; Collector <span class="title function_">toSet</span><span class="params">()</span>		<span class="comment">//把元素收集到Set集合中</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span>  Collector <span class="title function_">toMap</span><span class="params">(Function keyMapper,Function valueMapper)</span>		<span class="comment">//把元素收集到Map集合中</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CollectDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建List集合对象</span></span><br><span class="line">        List&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        //需求1：得到名字为3个字的流</span></span><br><span class="line"><span class="comment">        Stream&lt;String&gt; listStream = list.stream().filter(s -&gt; s.length() == 3);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        //需求2：把使用Stream流操作完毕的数据收集到List集合中并遍历</span></span><br><span class="line"><span class="comment">        List&lt;String&gt; names = listStream.collect(Collectors.toList());</span></span><br><span class="line"><span class="comment">        for(String name : names) &#123;</span></span><br><span class="line"><span class="comment">            System.out.println(name);</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建Set集合对象</span></span><br><span class="line">        Set&lt;Integer&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;Integer&gt;();</span><br><span class="line">        set.add(<span class="number">10</span>);</span><br><span class="line">        set.add(<span class="number">20</span>);</span><br><span class="line">        set.add(<span class="number">30</span>);</span><br><span class="line">        set.add(<span class="number">33</span>);</span><br><span class="line">        set.add(<span class="number">35</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        //需求3：得到年龄大于25的流</span></span><br><span class="line"><span class="comment">        Stream&lt;Integer&gt; setStream = set.stream().filter(age -&gt; age &gt; 25);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        //需求4：把使用Stream流操作完毕的数据收集到Set集合中并遍历</span></span><br><span class="line"><span class="comment">        Set&lt;Integer&gt; ages = setStream.collect(Collectors.toSet());</span></span><br><span class="line"><span class="comment">        for(Integer age : ages) &#123;</span></span><br><span class="line"><span class="comment">            System.out.println(age);</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        <span class="comment">//定义一个字符串数组，每一个字符串数据由姓名数据和年龄数据组合而成</span></span><br><span class="line">        String[] strArray = &#123;<span class="string">&quot;林青霞,30&quot;</span>, <span class="string">&quot;张曼玉,35&quot;</span>, <span class="string">&quot;王祖贤,33&quot;</span>, <span class="string">&quot;柳岩,25&quot;</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求5：得到字符串中年龄数据大于28的流</span></span><br><span class="line">        Stream&lt;String&gt; arrayStream = Stream.of(strArray).filter(s -&gt; Integer.parseInt(s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">1</span>]) &gt; <span class="number">28</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求6：把使用Stream流操作完毕的数据收集到Map集合中并遍历，字符串中的姓名作键，年龄作值</span></span><br><span class="line">        Map&lt;String, Integer&gt; map = arrayStream.collect(Collectors.toMap(s -&gt; s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">0</span>], s -&gt; Integer.parseInt(s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">1</span>])));</span><br><span class="line"></span><br><span class="line">        Set&lt;String&gt; keySet = map.keySet();</span><br><span class="line">        <span class="keyword">for</span> (String key : keySet) &#123;</span><br><span class="line">            <span class="type">Integer</span> <span class="variable">value</span> <span class="operator">=</span> map.get(key);</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="1-6-Stream流综合练习"   >
          <a href="#1-6-Stream流综合练习" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-6-Stream流综合练习" class="headerlink" title="1.6 Stream流综合练习"></a>1.6 Stream流综合练习</h2>
      <p>案例需求：现在有两个ArrayList集合，分别存储6名男演员名称和6名女演员名称，要求完成如下的操作</p>
<ul>
<li>男演员只要名字为3个字的前三人</li>
<li>女演员只要姓林的，并且不要第一个</li>
<li>把过滤后的男演员姓名和女演员姓名合并到一起</li>
<li>把上一步操作后的元素作为构造方法的参数创建演员对象,遍历数据</li>
</ul>
<p>演员类Actor已经提供，里面有一个成员变量，一个带参构造方法，以及成员变量对应的get&#x2F;set方法</p>
<p>代码实现:</p>
<p>演员类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Actor</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Actor</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合</span></span><br><span class="line">        ArrayList&lt;String&gt; manList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line">        manList.add(<span class="string">&quot;周润发&quot;</span>);</span><br><span class="line">        manList.add(<span class="string">&quot;成龙&quot;</span>);</span><br><span class="line">        manList.add(<span class="string">&quot;刘德华&quot;</span>);</span><br><span class="line">        manList.add(<span class="string">&quot;吴京&quot;</span>);</span><br><span class="line">        manList.add(<span class="string">&quot;周星驰&quot;</span>);</span><br><span class="line">        manList.add(<span class="string">&quot;李连杰&quot;</span>);</span><br><span class="line"></span><br><span class="line">        ArrayList&lt;String&gt; womanList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;String&gt;();</span><br><span class="line">        womanList.add(<span class="string">&quot;林心如&quot;</span>);</span><br><span class="line">        womanList.add(<span class="string">&quot;张曼玉&quot;</span>);</span><br><span class="line">        womanList.add(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        womanList.add(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        womanList.add(<span class="string">&quot;林志玲&quot;</span>);</span><br><span class="line">        womanList.add(<span class="string">&quot;王祖贤&quot;</span>);</span><br><span class="line">  </span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        //男演员只要名字为3个字的前三人</span></span><br><span class="line"><span class="comment">        Stream&lt;String&gt; manStream = manList.stream().filter(s -&gt; s.length() == 3).limit(3);</span></span><br><span class="line"><span class="comment">  </span></span><br><span class="line"><span class="comment">        //女演员只要姓林的，并且不要第一个</span></span><br><span class="line"><span class="comment">        Stream&lt;String&gt; womanStream = womanList.stream().filter(s -&gt; s.startsWith(&quot;林&quot;)).skip(1);</span></span><br><span class="line"><span class="comment">  </span></span><br><span class="line"><span class="comment">        //把过滤后的男演员姓名和女演员姓名合并到一起</span></span><br><span class="line"><span class="comment">        Stream&lt;String&gt; stream = Stream.concat(manStream, womanStream);</span></span><br><span class="line"><span class="comment">  </span></span><br><span class="line"><span class="comment">        //把上一步操作后的元素作为构造方法的参数创建演员对象,遍历数据</span></span><br><span class="line"><span class="comment">//        stream.map(Actor::new).forEach(System.out::println);</span></span><br><span class="line"><span class="comment">        stream.map(Actor::new).forEach(p -&gt; System.out.println(p.getName()));</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">  </span><br><span class="line">        Stream.concat(manList.stream().filter(s -&gt; s.length() == <span class="number">3</span>).limit(<span class="number">3</span>),</span><br><span class="line">                womanList.stream().filter(s -&gt; s.startsWith(<span class="string">&quot;林&quot;</span>)).skip(<span class="number">1</span>)).map(Actor::<span class="keyword">new</span>).</span><br><span class="line">                forEach(p -&gt; System.out.println(p.getName()));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="2-File类"   >
          <a href="#2-File类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-File类" class="headerlink" title="2.File类"></a>2.File类</h1>
      
        <h2 id="2-1-File类概述和构造方法"   >
          <a href="#2-1-File类概述和构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-File类概述和构造方法" class="headerlink" title="2.1 File类概述和构造方法"></a>2.1 File类概述和构造方法</h2>
      <p>File类介绍：</p>
<ul>
<li>它是文件和目录路径名的抽象表示</li>
<li>文件和目录是可以通过File封装成对象的</li>
<li>对于File而言,其封装的并不是一个真正存在的文件,仅仅是一个路径名而已.它可以是存在的,也可以是不存在的.将来是要通过具体的操作把这个路径的内容转换为具体存在的</li>
</ul>
<p>File类的构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">File(String pathname)		<span class="comment">//通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例</span></span><br><span class="line">  </span><br><span class="line">File(String parent, String child)		<span class="comment">//从父路径名字符串和子路径名字符串创建新的   File实例</span></span><br><span class="line">  </span><br><span class="line">File(File parent, String child)		<span class="comment">//从父抽象路径名和子路径名字符串创建新的   File实例</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//File(String pathname): 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast\\java.txt&quot;</span>);</span><br><span class="line">        System.out.println(f1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//File(String parent, String child): 从父路径名字符串和子路径名字符串创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast&quot;</span>,<span class="string">&quot;java.txt&quot;</span>);</span><br><span class="line">        System.out.println(f2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//File(File parent, String child): 从父抽象路径名和子路径名字符串创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast&quot;</span>);</span><br><span class="line">        <span class="type">File</span> <span class="variable">f4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(f3,<span class="string">&quot;java.txt&quot;</span>);</span><br><span class="line">        System.out.println(f4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-2-绝对路径和相对路径"   >
          <a href="#2-2-绝对路径和相对路径" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-绝对路径和相对路径" class="headerlink" title="2.2 绝对路径和相对路径"></a>2.2 绝对路径和相对路径</h2>
      <p>绝对路径：是一个完整的路径,从盘符开始</p>
<p>相对路径：是一个简化的路径,相对当前项目下的路径</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 是一个完整的路径,从盘符开始</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;D:\\itheima\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 是一个简化的路径,从当前项目根目录开始</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;a.txt&quot;</span>);</span><br><span class="line">        <span class="type">File</span> <span class="variable">file3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;模块名\\a.txt&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-3-File类创建功能"   >
          <a href="#2-3-File类创建功能" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-File类创建功能" class="headerlink" title="2.3 File类创建功能"></a>2.3 File类创建功能</h2>
      <p>方法分类：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">createNewFile</span><span class="params">()</span>		<span class="comment">//当具有该名称的文件不存在时，创建一个由该抽象路径名命名的新空文件</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">mkdir</span><span class="params">()</span>		<span class="comment">//创建由此抽象路径名命名的目录</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">mkdirs</span><span class="params">()</span>		<span class="comment">//创建由此抽象路径名命名的目录，包括任何必需但不存在的父目录</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//需求1：我要在E:\\itcast目录下创建一个文件java.txt</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast\\java.txt&quot;</span>);</span><br><span class="line">        System.out.println(f1.createNewFile());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：我要在E:\\itcast目录下创建一个目录JavaSE</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast\\JavaSE&quot;</span>);</span><br><span class="line">        System.out.println(f2.mkdir());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求3：我要在E:\\itcast目录下创建一个多级目录JavaWEB\\HTML</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast\\JavaWEB\\HTML&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f3.mkdir());</span></span><br><span class="line">        System.out.println(f3.mkdirs());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求4：我要在E:\\itcast目录下创建一个文件javase.txt</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast\\javase.txt&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f4.mkdir());</span></span><br><span class="line">        System.out.println(f4.createNewFile());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-4-File类删除功能"   >
          <a href="#2-4-File类删除功能" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-File类删除功能" class="headerlink" title="2.4 File类删除功能"></a>2.4 File类删除功能</h2>
      <p>方法分类:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">delete</span><span class="params">()</span>		<span class="comment">//删除由此抽象路径名表示的文件或目录</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"><span class="comment">//        File f1 = new File(&quot;E:\\itcast\\java.txt&quot;);</span></span><br><span class="line">        <span class="comment">//需求1：在当前模块目录下创建java.txt文件</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;myFile\\java.txt&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f1.createNewFile());</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求2：删除当前模块目录下的java.txt文件</span></span><br><span class="line">        System.out.println(f1.delete());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求3：在当前模块目录下创建itcast目录</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;myFile\\itcast&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f2.mkdir());</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求4：删除当前模块目录下的itcast目录</span></span><br><span class="line">        System.out.println(f2.delete());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求5：在当前模块下创建一个目录itcast,然后在该目录下创建一个文件java.txt</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;myFile\\itcast&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f3.mkdir());</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;myFile\\itcast\\java.txt&quot;</span>);</span><br><span class="line"><span class="comment">//        System.out.println(f4.createNewFile());</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求6：删除当前模块下的目录itcast</span></span><br><span class="line">        System.out.println(f4.delete());</span><br><span class="line">        System.out.println(f3.delete());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-5-File类判断和获取功能"   >
          <a href="#2-5-File类判断和获取功能" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-5-File类判断和获取功能" class="headerlink" title="2.5 File类判断和获取功能"></a>2.5 File类判断和获取功能</h2>
      <p>判断功能：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isDirectory</span><span class="params">()</span>		<span class="comment">//测试此抽象路径名表示的File是否为目录</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">isFile</span><span class="params">()</span>		<span class="comment">//测试此抽象路径名表示的File是否为文件</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">exists</span><span class="params">()</span>		<span class="comment">//测试此抽象路径名表示的File是否存在</span></span><br></pre></td></tr></table></div></figure>

<p>获取功能：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">getAbsolutePath</span><span class="params">()</span>		<span class="comment">//返回此抽象路径名的绝对路径名字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">getPath</span><span class="params">()</span>		<span class="comment">//将此抽象路径名转换为路径名字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span>		<span class="comment">//返回由此抽象路径名表示的文件或目录的名称</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> File[] listFiles()		<span class="comment">//返回此抽象路径名表示的目录中的文件和目录的File对象数组</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo04</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建一个File对象</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;myFile\\java.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public boolean isDirectory()：测试此抽象路径名表示的File是否为目录</span></span><br><span class="line"><span class="comment">//        public boolean isFile()：测试此抽象路径名表示的File是否为文件</span></span><br><span class="line"><span class="comment">//        public boolean exists()：测试此抽象路径名表示的File是否存在</span></span><br><span class="line">        System.out.println(f.isDirectory());</span><br><span class="line">        System.out.println(f.isFile());</span><br><span class="line">        System.out.println(f.exists());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public String getAbsolutePath()：返回此抽象路径名的绝对路径名字符串</span></span><br><span class="line"><span class="comment">//        public String getPath()：将此抽象路径名转换为路径名字符串</span></span><br><span class="line"><span class="comment">//        public String getName()：返回由此抽象路径名表示的文件或目录的名称</span></span><br><span class="line">        System.out.println(f.getAbsolutePath());</span><br><span class="line">        System.out.println(f.getPath());</span><br><span class="line">        System.out.println(f.getName());</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public File[] listFiles()：返回此抽象路径名表示的目录中的文件和目录的File对象数组</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;E:\\itcast&quot;</span>);</span><br><span class="line">        File[] fileArray = f2.listFiles();</span><br><span class="line">        <span class="keyword">for</span>(File file : fileArray) &#123;</span><br><span class="line"><span class="comment">//            System.out.println(file);</span></span><br><span class="line"><span class="comment">//            System.out.println(file.getName());</span></span><br><span class="line">            <span class="keyword">if</span>(file.isFile()) &#123;</span><br><span class="line">                System.out.println(file.getName());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="3-字节流"   >
          <a href="#3-字节流" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-字节流" class="headerlink" title="3.字节流"></a>3.字节流</h1>
      
        <h2 id="3-1-IO流概述和分类"   >
          <a href="#3-1-IO流概述和分类" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-IO流概述和分类" class="headerlink" title="3.1 IO流概述和分类"></a>3.1 IO流概述和分类</h2>
      <p>IO流介绍：</p>
<ul>
<li>IO：输入&#x2F;输出(Input&#x2F;Output)</li>
<li>流：是一种抽象概念,是对数据传输的总称.也就是说数据在设备间的传输称为流,流的本质是数据传输</li>
<li>IO流就是用来处理设备间数据传输问题的.常见的应用: 文件复制; 文件上传; 文件下载</li>
</ul>
<p>IO流的分类：</p>
<ul>
<li>按照数据的流向<ul>
<li>输入流：读数据</li>
<li>输出流：写数据</li>
</ul>
</li>
<li>按照数据类型来分<ul>
<li>字节流<ul>
<li>字节输入流</li>
<li>字节输出流</li>
</ul>
</li>
<li>字符流<ul>
<li>字符输入流</li>
<li>字符输出流</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>IO流的使用场景：</p>
<ul>
<li>如果操作的是纯文本文件,优先使用字符流</li>
<li>如果操作的是图片、视频、音频等二进制文件,优先使用字节流</li>
<li>如果不确定文件类型,优先使用字节流.字节流是万能的流</li>
</ul>

        <h2 id="3-2-字节流写数据"   >
          <a href="#3-2-字节流写数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-字节流写数据" class="headerlink" title="3.2 字节流写数据"></a>3.2 字节流写数据</h2>
      <p>字节流抽象基类：</p>
<ul>
<li><code>InputStream</code>：这个抽象类是表示字节输入流的所有类的超类</li>
<li><code>OutputStream</code>：这个抽象类是表示字节输出流的所有类的超类</li>
<li>子类名特点：子类名称都是以其父类名作为子类名的后缀</li>
</ul>
<p>字节输出流：</p>
<ul>
<li><code>FileOutputStream(String name)</code>：创建文件输出流以指定的名称写入文件</li>
</ul>
<p>使用字节输出流写数据的步骤</p>
<ul>
<li>创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)</li>
<li>调用字节输出流对象的写数据方法</li>
<li>释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)</li>
</ul>
<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileOutputStreamDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//创建字节输出流对象</span></span><br><span class="line">      	<span class="comment">/*</span></span><br><span class="line"><span class="comment">      		注意点:</span></span><br><span class="line"><span class="comment">      				1.如果文件不存在,会帮我们创建</span></span><br><span class="line"><span class="comment">      				2.如果文件存在,会把文件清空</span></span><br><span class="line"><span class="comment">      	*/</span></span><br><span class="line">      	<span class="comment">//FileOutputStream(String name)：创建文件输出流以指定的名称写入文件</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(int b)：将指定的字节写入此文件输出流</span></span><br><span class="line">        fos.write(<span class="number">97</span>);</span><br><span class="line"><span class="comment">//        fos.write(57);</span></span><br><span class="line"><span class="comment">//        fos.write(55);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//最后都要释放资源</span></span><br><span class="line">        <span class="comment">//void close()：关闭此文件输出流并释放与此流相关联的任何系统资源。</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-3-字节流写数据的三种方式"   >
          <a href="#3-3-字节流写数据的三种方式" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-3-字节流写数据的三种方式" class="headerlink" title="3.3 字节流写数据的三种方式"></a>3.3 字节流写数据的三种方式</h2>
      <p>写数据的方法分类:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">int</span> b)</span>		<span class="comment">//将指定的字节写入此文件输出流   一次写一个字节数据</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">byte</span>[] b)</span>		<span class="comment">//将 b.length字节从指定的字节数组写入此文件输出流   一次写一个字节数组数据</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">byte</span>[] b, <span class="type">int</span> off, <span class="type">int</span> len)</span>		<span class="comment">//将 len字节从指定的字节数组开始，从偏移量off开始写入此文件输出流   一次写一个字节数组的部分数据</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileOutputStreamDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//FileOutputStream(String name)：创建文件输出流以指定的名称写入文件</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>);</span><br><span class="line">        <span class="comment">//FileOutputStream(File file)：创建文件输出流以写入由指定的 File对象表示的文件</span></span><br><span class="line"><span class="comment">//        FileOutputStream fos = new FileOutputStream(new File(&quot;myByteStream\\fos.txt&quot;));</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(int b)：将指定的字节写入此文件输出流</span></span><br><span class="line"><span class="comment">//        fos.write(97);</span></span><br><span class="line"><span class="comment">//        fos.write(98);</span></span><br><span class="line"><span class="comment">//        fos.write(99);</span></span><br><span class="line"><span class="comment">//        fos.write(100);</span></span><br><span class="line"><span class="comment">//        fos.write(101);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        void write(byte[] b)：将 b.length字节从指定的字节数组写入此文件输出流</span></span><br><span class="line"><span class="comment">//        byte[] bys = &#123;97, 98, 99, 100, 101&#125;;</span></span><br><span class="line">        <span class="comment">//byte[] getBytes()：返回字符串对应的字节数组</span></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="string">&quot;abcde&quot;</span>.getBytes();</span><br><span class="line"><span class="comment">//        fos.write(bys);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(byte[] b, int off, int len)：将 len字节从指定的字节数组开始，从偏移量off开始写入此文件输出流</span></span><br><span class="line"><span class="comment">//        fos.write(bys,0,bys.length);</span></span><br><span class="line">        fos.write(bys,<span class="number">1</span>,<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-4-字节流写数据的两个小问题"   >
          <a href="#3-4-字节流写数据的两个小问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-4-字节流写数据的两个小问题" class="headerlink" title="3.4 字节流写数据的两个小问题"></a>3.4 字节流写数据的两个小问题</h2>
      <p>字节流写数据如何实现换行：</p>
<ul>
<li>windows:\r\n</li>
<li>linux:\n</li>
<li>mac:\r</li>
</ul>
<p>字节流写数据如何实现追加写入：</p>
<ul>
<li><code>public FileOutputStream(String name,boolean append)</code></li>
<li>创建文件输出流以指定的名称写入文件。如果第二个参数为true ，则字节将写入文件的末尾而不是开头</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileOutputStreamDemo03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//创建字节输出流对象</span></span><br><span class="line"><span class="comment">//        FileOutputStream fos = new FileOutputStream(&quot;myByteStream\\fos.txt&quot;);</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>,<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//写数据</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            fos.write(<span class="string">&quot;hello&quot;</span>.getBytes());</span><br><span class="line">            fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-5-字节流写数据加异常处理"   >
          <a href="#3-5-字节流写数据加异常处理" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-5-字节流写数据加异常处理" class="headerlink" title="3.5 字节流写数据加异常处理"></a>3.5 字节流写数据加异常处理</h2>
      <p>异常处理格式：</p>
<ul>
<li><p>try-catch-finally</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">	可能出现异常的代码;</span><br><span class="line">&#125;<span class="keyword">catch</span>(异常类名 变量名)&#123;</span><br><span class="line">	异常的处理代码;</span><br><span class="line">&#125;<span class="keyword">finally</span>&#123;</span><br><span class="line">	执行所有清除操作;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>finally特点</p>
<ul>
<li>被finally控制的语句一定会执行，除非JVM退出</li>
</ul>
</li>
</ul>
<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileOutputStreamDemo04</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//加入finally来实现释放资源</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            fos = <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>);</span><br><span class="line">            fos.write(<span class="string">&quot;hello&quot;</span>.getBytes());</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(fos != <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-6字节流读数据-一次读一个字节数据"   >
          <a href="#3-6字节流读数据-一次读一个字节数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-6字节流读数据-一次读一个字节数据" class="headerlink" title="3.6字节流读数据(一次读一个字节数据)"></a>3.6字节流读数据(一次读一个字节数据)</h2>
      <p>字节输入流：</p>
<ul>
<li>FileInputStream(String name)：通过打开与实际文件的连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名</li>
</ul>
<p>字节输入流读取数据的步骤：</p>
<ul>
<li>创建字节输入流对象</li>
<li>调用字节输入流对象的读数据方法</li>
<li>释放资源</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//创建字节输入流对象</span></span><br><span class="line">        <span class="comment">//FileInputStream(String name)</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span> by;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            fis.read()：读数据</span></span><br><span class="line"><span class="comment">            by=fis.read()：把读取到的数据赋值给by</span></span><br><span class="line"><span class="comment">            by != -1：判断读取到的数据是否是-1</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">while</span> ((by=fis.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print((<span class="type">char</span>)by);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-7-字节流复制文件"   >
          <a href="#3-7-字节流复制文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-7-字节流复制文件" class="headerlink" title="3.7 字节流复制文件"></a>3.7 字节流复制文件</h2>
      <p>案例需求：</p>
<p>​	把“E:\itcast\窗里窗外.txt”复制到模块目录下的“窗里窗外.txt”   (文件可以是任意文件)</p>
<p>实现步骤：</p>
<ol>
<li><p>复制文本文件，其实就把文本文件的内容从一个文件中读取出来(数据源)，然后写入到另一个文件中(目的地)</p>
</li>
<li><p>数据源：E:\itcast\窗里窗外.txt — 读数据 — InputStream — FileInputStream </p>
</li>
<li><p>目的地：myByteStream\窗里窗外.txt — 写数据 — OutputStream — FileOutputStream</p>
</li>
</ol>
<p>代码实现：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyTxtDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//根据数据源创建字节输入流对象</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;E:\\itcast\\窗里窗外.txt&quot;</span>);</span><br><span class="line">        <span class="comment">//根据目的地创建字节输出流对象</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\窗里窗外.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//读写数据，复制文本文件(一次读取一个字节，一次写入一个字节)</span></span><br><span class="line">        <span class="type">int</span> by;</span><br><span class="line">        <span class="keyword">while</span> ((by=fis.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            fos.write(by);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">        fis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-8-字节流读数据-一次读一个字节数组数据"   >
          <a href="#3-8-字节流读数据-一次读一个字节数组数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-8-字节流读数据-一次读一个字节数组数据" class="headerlink" title="3.8 字节流读数据(一次读一个字节数组数据)"></a>3.8 字节流读数据(一次读一个字节数组数据)</h2>
      <p>一次读一个字节数组的方法：</p>
<ul>
<li>public int read(byte[] b)：从输入流读取最多b.length个字节的数据</li>
<li>返回的是读入缓冲区的总字节数,也就是实际的读取字节个数</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//创建字节输入流对象</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;myByteStream\\fos.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>]; <span class="comment">//1024及其整数倍</span></span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">      	<span class="comment">//循环读取</span></span><br><span class="line">        <span class="keyword">while</span> ((len=fis.read(bys))!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print(<span class="keyword">new</span> <span class="title class_">String</span>(bys,<span class="number">0</span>,len));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="3-9-字节流复制文件"   >
          <a href="#3-9-字节流复制文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-9-字节流复制文件" class="headerlink" title="3.9 字节流复制文件"></a>3.9 字节流复制文件</h3>
      <p>案例需求：</p>
<p>​	把“E:\itcast\mn.jpg”复制到模块目录下的“mn.jpg”  (文件可以是任意文件去)</p>
<p>实现步骤：</p>
<ul>
<li>根据数据源创建字节输入流对象</li>
<li>根据目的地创建字节输出流对象</li>
<li>读写数据，复制图片(一次读取一个字节数组，一次写入一个字节数组)</li>
<li>释放资源</li>
</ul>
<p>代码实现</p>
  <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyJpgDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//根据数据源创建字节输入流对象</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;E:\\itcast\\mn.jpg&quot;</span>);</span><br><span class="line">        <span class="comment">//根据目的地创建字节输出流对象</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\mn.jpg&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//读写数据，复制图片(一次读取一个字节数组，一次写入一个字节数组)</span></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len=fis.read(bys))!=-<span class="number">1</span>) &#123;</span><br><span class="line">            fos.write(bys,<span class="number">0</span>,len);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">        fis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="4-字节缓冲流"   >
          <a href="#4-字节缓冲流" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-字节缓冲流" class="headerlink" title="4.字节缓冲流"></a>4.字节缓冲流</h1>
      
        <h2 id="4-1-字节缓冲流构造方法"   >
          <a href="#4-1-字节缓冲流构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-1-字节缓冲流构造方法" class="headerlink" title="4.1 字节缓冲流构造方法"></a>4.1 字节缓冲流构造方法</h2>
      <p>字节缓冲流介绍</p>
<ul>
<li><code>lBufferOutputStream</code>：该类实现缓冲输出流.通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用</li>
<li><code>lBufferedInputStream</code>：创建BufferedInputStream将创建一个内部缓冲区数组.当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节</li>
</ul>
<p>构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">BufferedOutputStream(OutputStream out)		<span class="comment">//创建字节缓冲输出流对象</span></span><br><span class="line">  </span><br><span class="line">BufferedInputStream(InputStream in)		<span class="comment">//创建字节缓冲输入流对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BufferStreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//字节缓冲输出流：BufferedOutputStream(OutputStream out)</span></span><br><span class="line"> </span><br><span class="line">        <span class="type">BufferedOutputStream</span> <span class="variable">bos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>(<span class="keyword">new</span> 				                                       <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\bos.txt&quot;</span>));</span><br><span class="line">        <span class="comment">//写数据</span></span><br><span class="line">        bos.write(<span class="string">&quot;hello\r\n&quot;</span>.getBytes());</span><br><span class="line">        bos.write(<span class="string">&quot;world\r\n&quot;</span>.getBytes());</span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        bos.close();</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">        <span class="comment">//字节缓冲输入流：BufferedInputStream(InputStream in)</span></span><br><span class="line">        <span class="type">BufferedInputStream</span> <span class="variable">bis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedInputStream</span>(<span class="keyword">new</span>                                                          <span class="title class_">FileInputStream</span>(<span class="string">&quot;myByteStream\\bos.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//一次读取一个字节数据</span></span><br><span class="line"><span class="comment">//        int by;</span></span><br><span class="line"><span class="comment">//        while ((by=bis.read())!=-1) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.print((char)by);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//一次读取一个字节数组数据</span></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len=bis.read(bys))!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print(<span class="keyword">new</span> <span class="title class_">String</span>(bys,<span class="number">0</span>,len));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        bis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="4-2-字节缓冲流复制视频"   >
          <a href="#4-2-字节缓冲流复制视频" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-字节缓冲流复制视频" class="headerlink" title="4.2 字节缓冲流复制视频"></a>4.2 字节缓冲流复制视频</h2>
      <p>案例需求：把“E:\itcast\字节流复制图片.avi”复制到模块目录下的“字节流复制图片.avi”</p>
<p>实现步骤：</p>
<ul>
<li>根据数据源创建字节输入流对象</li>
<li>根据目的地创建字节输出流对象</li>
<li>读写数据，复制视频</li>
<li>释放资源</li>
</ul>
<p>代码实现:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyAviDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//复制视频</span></span><br><span class="line"><span class="comment">//        method1();</span></span><br><span class="line">      	 method2();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//字节缓冲流一次读写一个字节数组</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">BufferedInputStream</span> <span class="variable">bis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedInputStream</span>(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;E:\\itcast\\字节流复制图片.avi&quot;</span>));</span><br><span class="line">        <span class="type">BufferedOutputStream</span> <span class="variable">bos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\字节流复制图片.avi&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len=bis.read(bys))!=-<span class="number">1</span>) &#123;</span><br><span class="line">            bos.write(bys,<span class="number">0</span>,len);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        bos.close();</span><br><span class="line">        bis.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//字节缓冲流一次读写一个字节</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">BufferedInputStream</span> <span class="variable">bis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedInputStream</span>(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;E:\\itcast\\字节流复制图片.avi&quot;</span>));</span><br><span class="line">        <span class="type">BufferedOutputStream</span> <span class="variable">bos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myByteStream\\字节流复制图片.avi&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span> by;</span><br><span class="line">        <span class="keyword">while</span> ((by=bis.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            bos.write(by);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        bos.close();</span><br><span class="line">        bis.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="5-字符流"   >
          <a href="#5-字符流" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-字符流" class="headerlink" title="5.字符流"></a>5.字符流</h1>
      
        <h2 id="5-1-为什么会出现字符流"   >
          <a href="#5-1-为什么会出现字符流" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-为什么会出现字符流" class="headerlink" title="5.1 为什么会出现字符流"></a>5.1 为什么会出现字符流</h2>
      <ul>
<li><p>字符流的介绍</p>
<p>由于字节流操作中文不是特别的方便，所以Java就提供字符流</p>
<p>字符流 &#x3D; 字节流 + 编码表</p>
</li>
<li><p>中文的字节存储方式</p>
<p>用字节流复制文本文件时，文本文件也会有中文，但是没有问题，原因是最终底层操作会自动进行字节拼接成中文，如何识别是中文的呢？</p>
<p>汉字在存储的时候，无论选择哪种编码存储，第一个字节都是负数</p>
</li>
</ul>

        <h2 id="5-2-编码表"   >
          <a href="#5-2-编码表" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-2-编码表" class="headerlink" title="5.2 编码表"></a>5.2 编码表</h2>
      <ul>
<li><p>什么是字符集</p>
<p>是一个系统支持的所有字符的集合，包括各国家文字、标点符号、图形符号、数字等</p>
<p>l计算机要准确的存储和识别各种字符集符号，就需要进行字符编码，一套字符集必然至少有一套字符编码。常见字符集有ASCII字符集、GBXXX字符集、Unicode字符集等</p>
</li>
<li><p>常见的字符集</p>
<ul>
<li><p>ASCII字符集：</p>
<p>lASCII：是基于拉丁字母的一套电脑编码系统，用于显示现代英语，主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号) </p>
<p>基本的ASCII字符集，使用7位表示一个字符，共128字符。ASCII的扩展字符集使用8位表示一个字符，共256字符，方便支持欧洲常用字符。是一个系统支持的所有字符的集合，包括各国家文字、标点符号、图形符号、数字等</p>
</li>
<li><p>GBXXX字符集：</p>
<p>GBK：最常用的中文码表。是在GB2312标准基础上的扩展规范，使用了双字节编码方案，共收录了21003个汉字，完全兼容GB2312标准，同时支持繁体汉字以及日韩汉字等</p>
</li>
<li><p>Unicode字符集：</p>
<p>UTF-8编码：可以用来表示Unicode标准中任意字符，它是电子邮件、网页及其他存储或传送文字的应用 中，优先采用的编码。互联网工程工作小组（IETF）要求所有互联网协议都必须支持UTF-8编码。它使用一至四个字节为每个字符编码</p>
<p>编码规则： </p>
<p>  128个US-ASCII字符，只需一个字节编码</p>
<p>  拉丁文等字符，需要二个字节编码</p>
<p>  大部分常用字（含中文），使用三个字节编码</p>
<p>  其他极少使用的Unicode辅助字符，使用四字节编码</p>
</li>
</ul>
</li>
</ul>

        <h2 id="5-3-字符串中的编码解码问题"   >
          <a href="#5-3-字符串中的编码解码问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-3-字符串中的编码解码问题" class="headerlink" title="5.3 字符串中的编码解码问题"></a>5.3 字符串中的编码解码问题</h2>
      <p>相关方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span>[] getBytes()		<span class="comment">//使用平台的默认字符集将该 String编码为一系列字节</span></span><br><span class="line">  </span><br><span class="line"><span class="type">byte</span>[] getBytes(String charsetName)		<span class="comment">//使用指定的字符集将该 String编码为一系列字节</span></span><br><span class="line">  </span><br><span class="line">String(<span class="type">byte</span>[] bytes)		<span class="comment">//使用平台的默认字符集解码指定的字节数组来创建字符串</span></span><br><span class="line">  </span><br><span class="line">String(<span class="type">byte</span>[] bytes, String charsetName)		<span class="comment">//通过指定的字符集解码指定的字节数组来创建字符串</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> UnsupportedEncodingException &#123;</span><br><span class="line">        <span class="comment">//定义一个字符串</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;中国&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//byte[] bys = s.getBytes(); //[-28, -72, -83, -27, -101, -67]</span></span><br><span class="line">        <span class="comment">//byte[] bys = s.getBytes(&quot;UTF-8&quot;); //[-28, -72, -83, -27, -101, -67]</span></span><br><span class="line">        <span class="type">byte</span>[] bys = s.getBytes(<span class="string">&quot;GBK&quot;</span>); <span class="comment">//[-42, -48, -71, -6]</span></span><br><span class="line">        System.out.println(Arrays.toString(bys));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//String ss = new String(bys);</span></span><br><span class="line">        <span class="comment">//String ss = new String(bys,&quot;UTF-8&quot;);</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">ss</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(bys,<span class="string">&quot;GBK&quot;</span>);</span><br><span class="line">        System.out.println(ss);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-4-字符流写数据"   >
          <a href="#5-4-字符流写数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-4-字符流写数据" class="headerlink" title="5.4 字符流写数据"></a>5.4 字符流写数据</h2>
      <p>介绍:</p>
<p>​	<code>Writer</code>: 用于写入字符流的抽象父类</p>
<p>​	<code>FileWriter:</code> 用于写入字符流的常用子类</p>
<p>构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">FileWriter(File file)		<span class="comment">//根据给定的 File 对象构造一个 FileWriter 对象</span></span><br><span class="line">  </span><br><span class="line">FileWriter(File file, <span class="type">boolean</span> append)		<span class="comment">//根据给定的 File 对象构造一个 FileWriter 对象</span></span><br><span class="line">  </span><br><span class="line">FileWriter(String fileName)		<span class="comment">//根据给定的文件名构造一个 FileWriter 对象</span></span><br><span class="line">  </span><br><span class="line">FileWriter(String fileName, <span class="type">boolean</span> append)		<span class="comment">//根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象</span></span><br></pre></td></tr></table></div></figure>

<p>成员方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">int</span> c)</span>		<span class="comment">//写一个字符</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">char</span>[] cbuf)</span>		<span class="comment">//写入一个字符数组</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(<span class="type">char</span>[] cbuf, <span class="type">int</span> off, <span class="type">int</span> len)</span>		<span class="comment">//写入字符数组的一部分</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(String str)</span>		<span class="comment">//写一个字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">write</span><span class="params">(String str, <span class="type">int</span> off, <span class="type">int</span> len)</span>		<span class="comment">//写一个字符串的一部分</span></span><br></pre></td></tr></table></div></figure>

<p>刷新和关闭的方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">flush()			<span class="comment">//刷新流，之后还可以继续写数据</span></span><br><span class="line">  </span><br><span class="line">close()		<span class="comment">//关闭流，释放资源，但是在关闭之前会先刷新流。一旦关闭，就不能再写数据</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OutputStreamWriterDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">FileWriter</span> <span class="variable">fw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileWriter</span>(<span class="string">&quot;myCharStream\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(int c)：写一个字符</span></span><br><span class="line"><span class="comment">//        fw.write(97);</span></span><br><span class="line"><span class="comment">//        fw.write(98);</span></span><br><span class="line"><span class="comment">//        fw.write(99);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void writ(char[] cbuf)：写入一个字符数组</span></span><br><span class="line">        <span class="type">char</span>[] chs = &#123;<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>&#125;;</span><br><span class="line"><span class="comment">//        fw.write(chs);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(char[] cbuf, int off, int len)：写入字符数组的一部分</span></span><br><span class="line"><span class="comment">//        fw.write(chs, 0, chs.length);</span></span><br><span class="line"><span class="comment">//        fw.write(chs, 1, 3);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(String str)：写一个字符串</span></span><br><span class="line"><span class="comment">//        fw.write(&quot;abcde&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//void write(String str, int off, int len)：写一个字符串的一部分</span></span><br><span class="line"><span class="comment">//        fw.write(&quot;abcde&quot;, 0, &quot;abcde&quot;.length());</span></span><br><span class="line">        fw.write(<span class="string">&quot;abcde&quot;</span>, <span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fw.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-5-字符流读数据"   >
          <a href="#5-5-字符流读数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-5-字符流读数据" class="headerlink" title="5.5 字符流读数据"></a>5.5 字符流读数据</h2>
      <p>介绍:</p>
<p><code>Reader</code>: 用于读取字符流的抽象父类</p>
<p><code>FileReader</code>: 用于读取字符流的常用子类</p>
<p>构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FileReader(File file)		<span class="comment">//在给定从中读取数据的 File 的情况下创建一个新 FileReader</span></span><br><span class="line">  </span><br><span class="line">FileReader(String fileName)		<span class="comment">//在给定从中读取数据的文件名的情况下创建一个新 FileReader</span></span><br></pre></td></tr></table></div></figure>

<p>成员方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">read</span><span class="params">()</span>		<span class="comment">//一次读一个字符数据</span></span><br><span class="line">  </span><br><span class="line"><span class="type">int</span> <span class="title function_">read</span><span class="params">(<span class="type">char</span>[] cbuf)</span>		<span class="comment">//一次读一个字符数组数据</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InputStreamReaderDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">   </span><br><span class="line">        <span class="type">FileReader</span> <span class="variable">fr</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileReader</span>(<span class="string">&quot;myCharStream\\b.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//int read()：一次读一个字符数据</span></span><br><span class="line"><span class="comment">//        int ch;</span></span><br><span class="line"><span class="comment">//        while ((ch=fr.read())!=-1) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.print((char)ch);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//int read(char[] cbuf)：一次读一个字符数组数据</span></span><br><span class="line">        <span class="type">char</span>[] chs = <span class="keyword">new</span> <span class="title class_">char</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len = fr.read(chs)) != -<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print(<span class="keyword">new</span> <span class="title class_">String</span>(chs, <span class="number">0</span>, len));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        fr.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-6-字符流用户注册案例"   >
          <a href="#5-6-字符流用户注册案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-字符流用户注册案例" class="headerlink" title="5.6 字符流用户注册案例"></a>5.6 字符流用户注册案例</h2>
      <p>案例需求：将键盘录入的用户名和密码保存到本地实现永久化存储</p>
<p>实现步骤：</p>
<ul>
<li>获取用户输入的用户名和密码</li>
<li>将用户输入的用户名和密码写入到本地文件中</li>
<li>关流,释放资源</li>
</ul>
<p>代码实现：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CharStreamDemo8</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//需求: 将键盘录入的用户名和密码保存到本地实现永久化存储</span></span><br><span class="line">        <span class="comment">//要求：用户名独占一行，密码独占一行</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//分析：</span></span><br><span class="line">        <span class="comment">//1，实现键盘录入，把用户名和密码录入进来</span></span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请录入用户名&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> sc.next();</span><br><span class="line">        System.out.println(<span class="string">&quot;请录入密码&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> sc.next();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.分别把用户名和密码写到本地文件。</span></span><br><span class="line">        <span class="type">FileWriter</span> <span class="variable">fw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileWriter</span>(<span class="string">&quot;charstream\\a.txt&quot;</span>);</span><br><span class="line">        <span class="comment">//将用户名和密码写到文件中</span></span><br><span class="line">        fw.write(username);</span><br><span class="line">        <span class="comment">//表示写出一个回车换行符 windows \r\n  MacOS \r  Linux \n</span></span><br><span class="line">        fw.write(<span class="string">&quot;\r\n&quot;</span>);</span><br><span class="line">        fw.write(password);</span><br><span class="line">        <span class="comment">//刷新流</span></span><br><span class="line">        fw.flush();</span><br><span class="line">        <span class="comment">//3.关流,释放资源</span></span><br><span class="line">        fw.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-7-字符缓冲流"   >
          <a href="#5-7-字符缓冲流" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-7-字符缓冲流" class="headerlink" title="5.7 字符缓冲流"></a>5.7 字符缓冲流</h2>
      <p>字符缓冲流介绍：</p>
<ul>
<li><p><code>BufferedWrite</code>r：将文本写入字符输出流，缓冲字符，以提供单个字符，数组和字符串的高效写入，可以指定缓冲区大小，或者可以接受默认大小。默认值足够大，可用于大多数用途</p>
</li>
<li><p><code>BufferedReader</code>：从字符输入流读取文本，缓冲字符，以提供字符，数组和行的高效读取，可以指定缓冲区大小，或者可以使用默认大小。 默认值足够大，可用于大多数用途</p>
</li>
</ul>
<p>构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">BufferedWriter(Writer out)		<span class="comment">//创建字符缓冲输出流对象</span></span><br><span class="line">  </span><br><span class="line">  	</span><br><span class="line">BufferedReader(Reader in)		<span class="comment">//创建字符缓冲输入流对象</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BufferedStreamDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//BufferedWriter(Writer out)</span></span><br><span class="line">        <span class="type">BufferedWriter</span> <span class="variable">bw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedWriter</span>(<span class="keyword">new</span>                                                            <span class="title class_">FileWriter</span>(<span class="string">&quot;myCharStream\\bw.txt&quot;</span>));</span><br><span class="line">        bw.write(<span class="string">&quot;hello\r\n&quot;</span>);</span><br><span class="line">        bw.write(<span class="string">&quot;world\r\n&quot;</span>);</span><br><span class="line">        bw.close();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//BufferedReader(Reader in)</span></span><br><span class="line">        <span class="type">BufferedReader</span> <span class="variable">br</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span>                                                           <span class="title class_">FileReader</span>(<span class="string">&quot;myCharStream\\bw.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//一次读取一个字符数据</span></span><br><span class="line"><span class="comment">//        int ch;</span></span><br><span class="line"><span class="comment">//        while ((ch=br.read())!=-1) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.print((char)ch);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//一次读取一个字符数组数据</span></span><br><span class="line">        <span class="type">char</span>[] chs = <span class="keyword">new</span> <span class="title class_">char</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len=br.read(chs))!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print(<span class="keyword">new</span> <span class="title class_">String</span>(chs,<span class="number">0</span>,len));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        br.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-8-字符缓冲流特有功能"   >
          <a href="#5-8-字符缓冲流特有功能" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-8-字符缓冲流特有功能" class="headerlink" title="5.8 字符缓冲流特有功能"></a>5.8 字符缓冲流特有功能</h2>
      <p>方法介绍:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//BufferedWriter：</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">newLine</span><span class="params">()</span>		<span class="comment">//写一行行分隔符，行分隔符字符串由系统属性定义</span></span><br><span class="line">  </span><br><span class="line"><span class="comment">//BufferedReader:  </span></span><br><span class="line">String <span class="title function_">readLine</span><span class="params">()</span>		<span class="comment">//读一行文字。 结果包含行的内容的字符串，不包括任何行终止字符如果流的结尾已经到达，则为null</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BufferedStreamDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建字符缓冲输出流</span></span><br><span class="line">        <span class="type">BufferedWriter</span> <span class="variable">bw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedWriter</span>(<span class="keyword">new</span>                                                          <span class="title class_">FileWriter</span>(<span class="string">&quot;myCharStream\\bw.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//写数据</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">            bw.write(<span class="string">&quot;hello&quot;</span> + i);</span><br><span class="line">            <span class="comment">//bw.write(&quot;\r\n&quot;);</span></span><br><span class="line">            bw.newLine();</span><br><span class="line">            bw.flush();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        bw.close();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建字符缓冲输入流</span></span><br><span class="line">        <span class="type">BufferedReader</span> <span class="variable">br</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span>                                                          <span class="title class_">FileReader</span>(<span class="string">&quot;myCharStream\\bw.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        String line;</span><br><span class="line">        <span class="keyword">while</span> ((line=br.readLine())!=<span class="literal">null</span>) &#123;</span><br><span class="line">            System.out.println(line);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        br.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-9-字符缓冲流操作文件中数据排序案例"   >
          <a href="#5-9-字符缓冲流操作文件中数据排序案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-9-字符缓冲流操作文件中数据排序案例" class="headerlink" title="5.9 字符缓冲流操作文件中数据排序案例"></a>5.9 字符缓冲流操作文件中数据排序案例</h2>
      <p>案例需求：使用字符缓冲流读取文件中的数据，排序后再次写到本地文件</p>
<p>实现步骤：</p>
<ol>
<li>将文件中的数据读取到程序中</li>
<li>对读取到的数据进行处理</li>
<li>将处理后的数据添加到集合中</li>
<li>对集合中的数据进行排序</li>
<li>将排序后的集合中的数据写入到文件中</li>
</ol>
<p>代码实现：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CharStreamDemo14</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//需求：读取文件中的数据，排序后再次写到本地文件</span></span><br><span class="line">        <span class="comment">//分析：</span></span><br><span class="line">        <span class="comment">//1.要把文件中的数据读取进来。</span></span><br><span class="line">        <span class="type">BufferedReader</span> <span class="variable">br</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">FileReader</span>(<span class="string">&quot;charstream\\sort.txt&quot;</span>));</span><br><span class="line">        <span class="comment">//输出流一定不能写在这里，因为会清空文件中的内容</span></span><br><span class="line">        <span class="comment">//BufferedWriter bw = new BufferedWriter(new FileWriter(&quot;charstream\\sort.txt&quot;));</span></span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">line</span> <span class="operator">=</span> br.readLine();</span><br><span class="line">        System.out.println(<span class="string">&quot;读取到的数据为&quot;</span> + line);</span><br><span class="line">        br.close();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.按照空格进行切割</span></span><br><span class="line">        String[] split = line.split(<span class="string">&quot; &quot;</span>);<span class="comment">//9 1 2 5 3 10 4 6 7 8</span></span><br><span class="line">        <span class="comment">//3.把字符串类型的数组变成int类型</span></span><br><span class="line">        <span class="type">int</span> [] arr = <span class="keyword">new</span> <span class="title class_">int</span>[split.length];</span><br><span class="line">        <span class="comment">//遍历split数组，可以进行类型转换。</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; split.length; i++) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">smallStr</span> <span class="operator">=</span> split[i];</span><br><span class="line">            <span class="comment">//类型转换</span></span><br><span class="line">            <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> Integer.parseInt(smallStr);</span><br><span class="line">            <span class="comment">//把转换后的结果存入到arr中</span></span><br><span class="line">            arr[i] = number;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//4.排序</span></span><br><span class="line">        Arrays.sort(arr);</span><br><span class="line">        System.out.println(Arrays.toString(arr));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//5.把排序之后结果写回到本地 1 2 3 4...</span></span><br><span class="line">        <span class="type">BufferedWriter</span> <span class="variable">bw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedWriter</span>(<span class="keyword">new</span> <span class="title class_">FileWriter</span>(<span class="string">&quot;charstream\\sort.txt&quot;</span>));</span><br><span class="line">        <span class="comment">//写出</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            bw.write(arr[i] + <span class="string">&quot; &quot;</span>);</span><br><span class="line">            bw.flush();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        bw.close();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-10-IO流小结"   >
          <a href="#5-10-IO流小结" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-10-IO流小结" class="headerlink" title="5.10 IO流小结"></a>5.10 IO流小结</h2>
      <p><img src="https://s2.loli.net/2022/09/14/1ErPyf4tNpWFZIq.png" alt="IO流小结.png"></p>

        <h1 id="6-转换流"   >
          <a href="#6-转换流" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-转换流" class="headerlink" title="6.转换流"></a>6.转换流</h1>
      
        <h2 id="6-1-字符流中和编码解码问题相关的两个类"   >
          <a href="#6-1-字符流中和编码解码问题相关的两个类" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-字符流中和编码解码问题相关的两个类" class="headerlink" title="6.1 字符流中和编码解码问题相关的两个类"></a>6.1 字符流中和编码解码问题相关的两个类</h2>
      <ul>
<li><p><code>InputStreamReader</code>：是从字节流到字符流的桥梁,父类是<code>Reader</code></p>
<p>​	它读取字节，并使用指定的编码将其解码为字符</p>
<p>​	它使用的字符集可以由名称指定，也可以被明确指定，或者可以接受平台的默认字符集</p>
</li>
<li><p><code>OutputStreamWriter</code>：是从字符流到字节流的桥梁,父类是<code>Writer</code></p>
<p>​	是从字符流到字节流的桥梁，使用指定的编码将写入的字符编码为字节</p>
<p>​	它使用的字符集可以由名称指定，也可以被明确指定，或者可以接受平台的默认字符集</p>
</li>
</ul>

        <h2 id="6-2-转换流读写数据"   >
          <a href="#6-2-转换流读写数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-转换流读写数据" class="headerlink" title="6.2 转换流读写数据"></a>6.2 转换流读写数据</h2>
      <p>构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">InputStreamReader(InputStream in)		<span class="comment">//使用默认字符编码创建InputStreamReader对象</span></span><br><span class="line">  </span><br><span class="line">InputStreamReader(InputStream in,String chatset)		<span class="comment">//使用指定的字符编码创建InputStreamReader对象</span></span><br><span class="line">  </span><br><span class="line">OutputStreamWriter(OutputStream out)		<span class="comment">//使用默认字符编码创建OutputStreamWriter对象</span></span><br><span class="line">  </span><br><span class="line">OutputStreamWriter(OutputStream out,String charset)		<span class="comment">//使用指定的字符编码创建OutputStreamWriter对象</span></span><br></pre></td></tr></table></div></figure>

<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ConversionStreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//OutputStreamWriter osw = new OutputStreamWriter(new                                             FileOutputStream(&quot;myCharStream\\osw.txt&quot;));</span></span><br><span class="line">        <span class="type">OutputStreamWriter</span> <span class="variable">osw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OutputStreamWriter</span>(<span class="keyword">new</span>                                              <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myCharStream\\osw.txt&quot;</span>),<span class="string">&quot;GBK&quot;</span>);</span><br><span class="line">        osw.write(<span class="string">&quot;中国&quot;</span>);</span><br><span class="line">        osw.close();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//InputStreamReader isr = new InputStreamReader(new 	                                         FileInputStream(&quot;myCharStream\\osw.txt&quot;));</span></span><br><span class="line">        <span class="type">InputStreamReader</span> <span class="variable">isr</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(<span class="keyword">new</span>                                                 <span class="title class_">FileInputStream</span>(<span class="string">&quot;myCharStream\\osw.txt&quot;</span>),<span class="string">&quot;GBK&quot;</span>);</span><br><span class="line">        <span class="comment">//一次读取一个字符数据</span></span><br><span class="line">        <span class="type">int</span> ch;</span><br><span class="line">        <span class="keyword">while</span> ((ch=isr.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print((<span class="type">char</span>)ch);</span><br><span class="line">        &#125;</span><br><span class="line">        isr.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="7-对象操作流"   >
          <a href="#7-对象操作流" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-对象操作流" class="headerlink" title="7.对象操作流"></a>7.对象操作流</h1>
      
        <h2 id="7-1-对象序列化流"   >
          <a href="#7-1-对象序列化流" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-1-对象序列化流" class="headerlink" title="7.1 对象序列化流"></a>7.1 对象序列化流</h2>
      <p>对象序列化介绍:</p>
<ul>
<li>对象序列化：就是将对象保存到磁盘中，或者在网络中传输对象</li>
<li>这种机制就是使用一个字节序列表示一个对象，该字节序列包含：对象的类型、对象的数据和对象中存储的属性等信息</li>
<li>字节序列写到文件之后，相当于文件中持久保存了一个对象的信息</li>
<li>反之，该字节序列还可以从文件中读取回来，重构对象，对它进行反序列化</li>
</ul>
<p>对象序列化流： <code>ObjectOutputStream</code></p>
<p>​	将Java对象的原始数据类型和图形写入<code>OutputStream</code>。 可以使用<code>ObjectInputStream</code>读取（重构）对象。 可以通过使用流的文件来实现对象的持久存储。 如果流是网络套接字流，则可以在另一个主机上或另一个进程中重构对象 </p>
<p>构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ObjectOutputStream(OutputStream out)		<span class="comment">//创建一个写入指定的OutputStream的ObjectOutputStream</span></span><br></pre></td></tr></table></div></figure>

<p>序列化对象的方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">writeObject</span><span class="params">(Object obj)</span>		<span class="comment">//将指定的对象写入ObjectOutputStream</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectOutputStreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//ObjectOutputStream(OutputStream out)：创建一个写入指定的OutputStream的ObjectOutputStream</span></span><br><span class="line">        <span class="type">ObjectOutputStream</span> <span class="variable">oos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectOutputStream</span>(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myOtherStream\\oos.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;佟丽娅&quot;</span>,<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void writeObject(Object obj)：将指定的对象写入ObjectOutputStream</span></span><br><span class="line">        oos.writeObject(s);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放资源</span></span><br><span class="line">        oos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>注意事项:</p>
<ul>
<li>一个对象要想被序列化，该对象所属的类必须必须实现<code>Serializable</code> 接口</li>
<li><code>Serializable</code>是一个标记接口，实现该接口，不需要重写任何方法</li>
</ul>

        <h2 id="7-2-对象反序列化流"   >
          <a href="#7-2-对象反序列化流" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-2-对象反序列化流" class="headerlink" title="7.2 对象反序列化流"></a>7.2 对象反序列化流</h2>
      <p>对象反序列化流： <code>ObjectInputStream</code></p>
<ul>
<li><code>ObjectInputStream</code>反序列化先前使用<code>ObjectOutputStream</code>编写的原始数据和对象</li>
</ul>
<p>构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ObjectInputStream(InputStream in)		<span class="comment">//创建从指定的InputStream读取的ObjectInputStream</span></span><br></pre></td></tr></table></div></figure>

<p>反序列化对象的方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Object <span class="title function_">readObject</span><span class="params">()</span>			<span class="comment">//从ObjectInputStream读取一个对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectInputStreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, ClassNotFoundException &#123;</span><br><span class="line">        <span class="comment">//ObjectInputStream(InputStream in)：创建从指定的InputStream读取的ObjectInputStream</span></span><br><span class="line">        <span class="type">ObjectInputStream</span> <span class="variable">ois</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectInputStream</span>(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;myOtherStream\\oos.txt&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Object readObject()：从ObjectInputStream读取一个对象</span></span><br><span class="line">        <span class="type">Object</span> <span class="variable">obj</span> <span class="operator">=</span> ois.readObject();</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> (Student) obj;</span><br><span class="line">        System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line"></span><br><span class="line">        ois.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="7-3-serialVersionUID-amp-transient"   >
          <a href="#7-3-serialVersionUID-amp-transient" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-3-serialVersionUID-amp-transient" class="headerlink" title="7.3 serialVersionUID&amp;transient"></a>7.3 serialVersionUID&amp;transient</h2>
      <p>serialVersionUID:</p>
<ul>
<li>用对象序列化流序列化了一个对象后，假如我们修改了对象所属的类文件，读取数据会不会出问题呢？<ul>
<li>会出问题，会抛出InvalidClassException异常</li>
</ul>
</li>
<li>如果出问题了，如何解决呢？<ul>
<li>重新序列化</li>
<li>给对象所属的类加一个serialVersionUID <ul>
<li>private static final long serialVersionUID &#x3D; 42L;</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>transient:</p>
<ul>
<li>如果一个对象中的某个成员变量的值不想被序列化，又该如何实现呢？<ul>
<li>给该成员变量加transient关键字修饰，该关键字标记的成员变量不参与序列化过程</li>
</ul>
</li>
</ul>
<p>示例代码:</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">long</span> <span class="variable">serialVersionUID</span> <span class="operator">=</span> <span class="number">42L</span>;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"><span class="comment">//    private int age;</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">transient</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    @Override</span></span><br><span class="line"><span class="comment">//    public String toString() &#123;</span></span><br><span class="line"><span class="comment">//        return &quot;Student&#123;&quot; +</span></span><br><span class="line"><span class="comment">//                &quot;name=&#x27;&quot; + name + &#x27;\&#x27;&#x27; +</span></span><br><span class="line"><span class="comment">//                &quot;, age=&quot; + age +</span></span><br><span class="line"><span class="comment">//                &#x27;&#125;&#x27;;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectStreamDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, ClassNotFoundException &#123;</span><br><span class="line"><span class="comment">//        write();</span></span><br><span class="line">        read();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//反序列化</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">read</span><span class="params">()</span> <span class="keyword">throws</span> IOException, ClassNotFoundException &#123;</span><br><span class="line">        <span class="type">ObjectInputStream</span> <span class="variable">ois</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectInputStream</span>(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;myOtherStream\\oos.txt&quot;</span>));</span><br><span class="line">        <span class="type">Object</span> <span class="variable">obj</span> <span class="operator">=</span> ois.readObject();</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> (Student) obj;</span><br><span class="line">        System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line">        ois.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//序列化</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">write</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">ObjectOutputStream</span> <span class="variable">oos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectOutputStream</span>(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;myOtherStream\\oos.txt&quot;</span>));</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;佟丽娅&quot;</span>, <span class="number">30</span>);</span><br><span class="line">        oos.writeObject(s);</span><br><span class="line">        oos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="7-4-对象操作流练习"   >
          <a href="#7-4-对象操作流练习" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-4-对象操作流练习" class="headerlink" title="7.4 对象操作流练习"></a>7.4 对象操作流练习</h2>
      <p>案例需求：创建多个学生类对象写到文件中,再次读取到内存中</p>
<p>实现步骤：</p>
<ol>
<li>创建序列化流对象</li>
<li>创建多个学生对象</li>
<li>将学生对象添加到集合中</li>
<li>将集合对象序列化到文件中</li>
<li>创建反序列化流对象</li>
<li>将文件中的对象数据,读取到内存中</li>
</ol>
<p>代码实现:</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">long</span> <span class="variable">serialVersionUID</span> <span class="operator">=</span> <span class="number">2L</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo03</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *  read():</span></span><br><span class="line"><span class="comment">     *      读取到文件末尾返回值是 -1</span></span><br><span class="line"><span class="comment">     *  readLine():</span></span><br><span class="line"><span class="comment">     *      读取到文件的末尾返回值 null</span></span><br><span class="line"><span class="comment">     *  readObject():</span></span><br><span class="line"><span class="comment">     *      读取到文件的末尾 直接抛出异常</span></span><br><span class="line"><span class="comment">     *  如果要序列化的对象有多个,不建议直接将多个对象序列化到文件中,因为反序列化时容易出异常</span></span><br><span class="line"><span class="comment">     *      建议: 将要序列化的多个对象存储到集合中,然后将集合序列化到文件中</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">/*// 序列化</span></span><br><span class="line"><span class="comment">        //1.创建序列化流对象</span></span><br><span class="line"><span class="comment">        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(&quot;myCode\\oos.txt&quot;));</span></span><br><span class="line"><span class="comment">        ArrayList&lt;Student&gt; arrayList = new ArrayList&lt;&gt;();</span></span><br><span class="line"><span class="comment">        //2.创建多个学生对象</span></span><br><span class="line"><span class="comment">        Student s = new Student(&quot;佟丽娅&quot;,30);</span></span><br><span class="line"><span class="comment">        Student s01 = new Student(&quot;佟丽娅&quot;,30);</span></span><br><span class="line"><span class="comment">        //3.将学生对象添加到集合中</span></span><br><span class="line"><span class="comment">        arrayList.add(s);</span></span><br><span class="line"><span class="comment">        arrayList.add(s01);</span></span><br><span class="line"><span class="comment">        //4.将集合对象序列化到文件中</span></span><br><span class="line"><span class="comment">        oos.writeObject(arrayList);</span></span><br><span class="line"><span class="comment">        oos.close();*/</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 反序列化</span></span><br><span class="line">      	<span class="comment">//5.创建反序列化流对象</span></span><br><span class="line">        <span class="type">ObjectInputStream</span> <span class="variable">ois</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectInputStream</span>(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;myCode\\oos.txt&quot;</span>));</span><br><span class="line">      	<span class="comment">//6.将文件中的对象数据,读取到内存中</span></span><br><span class="line">        <span class="type">Object</span> <span class="variable">obj</span> <span class="operator">=</span> ois.readObject();</span><br><span class="line">        ArrayList&lt;Student&gt; arrayList = (ArrayList&lt;Student&gt;)obj;</span><br><span class="line">        ois.close();</span><br><span class="line">        <span class="keyword">for</span> (Student s : arrayList) &#123;</span><br><span class="line">            System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="8-Properties集合"   >
          <a href="#8-Properties集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-Properties集合" class="headerlink" title="8.Properties集合"></a>8.Properties集合</h1>
      
        <h2 id="8-1-Properties作为Map集合的使用"   >
          <a href="#8-1-Properties作为Map集合的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-Properties作为Map集合的使用" class="headerlink" title="8.1 Properties作为Map集合的使用"></a>8.1 Properties作为Map集合的使用</h2>
      <p>Properties介绍：</p>
<ul>
<li>是一个Map体系的集合类</li>
<li>Properties可以保存到流中或从流中加载</li>
<li>属性列表中的每个键及其对应的值都是一个字符串</li>
</ul>
<p>Properties基本使用：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line"><span class="comment">//        Properties&lt;String,String&gt; prop = new Properties&lt;String,String&gt;(); //错误</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//存储元素</span></span><br><span class="line">        prop.put(<span class="string">&quot;itheima001&quot;</span>, <span class="string">&quot;佟丽娅&quot;</span>);</span><br><span class="line">        prop.put(<span class="string">&quot;itheima002&quot;</span>, <span class="string">&quot;赵丽颖&quot;</span>);</span><br><span class="line">        prop.put(<span class="string">&quot;itheima003&quot;</span>, <span class="string">&quot;刘诗诗&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合</span></span><br><span class="line">        Set&lt;Object&gt; keySet = prop.keySet();</span><br><span class="line">        <span class="keyword">for</span> (Object key : keySet) &#123;</span><br><span class="line">            <span class="type">Object</span> <span class="variable">value</span> <span class="operator">=</span> prop.get(key);</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-2-Properties作为Map集合的特有方法"   >
          <a href="#8-2-Properties作为Map集合的特有方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-Properties作为Map集合的特有方法" class="headerlink" title="8.2 Properties作为Map集合的特有方法"></a>8.2 Properties作为Map集合的特有方法</h2>
      <p>特有方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Object <span class="title function_">setProperty</span><span class="params">(String key, String value)</span></span><br><span class="line"><span class="comment">//设置集合的键和值，都是String类型，底层调用Hashtable方法 put</span></span><br><span class="line">  </span><br><span class="line">String <span class="title function_">getProperty</span><span class="params">(String key)</span></span><br><span class="line"><span class="comment">//使用此属性列表中指定的键搜索属性</span></span><br><span class="line">  </span><br><span class="line">Set&lt;String&gt; <span class="title function_">stringPropertyNames</span><span class="params">()</span></span><br><span class="line"><span class="comment">//从该属性列表中返回一个不可修改的键集，其中键及其对应的值是字符串</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Object setProperty(String key, String value)：设置集合的键和值，都是String类型</span></span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima001&quot;</span>, <span class="string">&quot;佟丽娅&quot;</span>);</span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima002&quot;</span>, <span class="string">&quot;赵丽颖&quot;</span>);</span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima003&quot;</span>, <span class="string">&quot;刘诗诗&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//String getProperty(String key)：使用此属性列表中指定的键搜索属性</span></span><br><span class="line"><span class="comment">//        System.out.println(prop.getProperty(&quot;itheima001&quot;));</span></span><br><span class="line"><span class="comment">//        System.out.println(prop.getProperty(&quot;itheima0011&quot;));</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        System.out.println(prop);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//Set&lt;String&gt; stringPropertyNames()：从该属性列表中返回一个不可修改的键集，其中键及其对应的值是字符串</span></span><br><span class="line">        Set&lt;String&gt; names = prop.stringPropertyNames();</span><br><span class="line">        <span class="keyword">for</span> (String key : names) &#123;</span><br><span class="line"><span class="comment">//            System.out.println(key);</span></span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> prop.getProperty(key);</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-3-Properties和IO流相结合的方法"   >
          <a href="#8-3-Properties和IO流相结合的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-Properties和IO流相结合的方法" class="headerlink" title="8.3 Properties和IO流相结合的方法"></a>8.3 Properties和IO流相结合的方法</h2>
      <p>和IO流结合的方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">load</span><span class="params">(Reader reader)</span>		<span class="comment">//从输入字符流读取属性列表（键和元素对）</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">store</span><span class="params">(Writer writer, String comments)</span></span><br><span class="line"><span class="comment">//将此属性列表（键和元素对）写入此Properties表中，以适合使用load(Reader)方法的格式写入输出字符流</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">//把集合中的数据保存到文件</span></span><br><span class="line"><span class="comment">//        myStore();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//把文件中的数据加载到集合</span></span><br><span class="line">        myLoad();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">myLoad</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void load(Reader reader)：</span></span><br><span class="line">        <span class="type">FileReader</span> <span class="variable">fr</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileReader</span>(<span class="string">&quot;myOtherStream\\fw.txt&quot;</span>);</span><br><span class="line">        prop.load(fr);</span><br><span class="line">        fr.close();</span><br><span class="line"></span><br><span class="line">        System.out.println(prop);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">myStore</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima001&quot;</span>,<span class="string">&quot;佟丽娅&quot;</span>);</span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima002&quot;</span>,<span class="string">&quot;赵丽颖&quot;</span>);</span><br><span class="line">        prop.setProperty(<span class="string">&quot;itheima003&quot;</span>,<span class="string">&quot;刘诗诗&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//void store(Writer writer, String comments)：</span></span><br><span class="line">        <span class="type">FileWriter</span> <span class="variable">fw</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileWriter</span>(<span class="string">&quot;myOtherStream\\fw.txt&quot;</span>);</span><br><span class="line">        prop.store(fw,<span class="literal">null</span>);</span><br><span class="line">        fw.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-4-Properties集合练习"   >
          <a href="#8-4-Properties集合练习" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-Properties集合练习" class="headerlink" title="8.4 Properties集合练习"></a>8.4 Properties集合练习</h2>
      <p>案例需求：在Properties文件中手动写上姓名和年龄,读取到集合中,将该数据封装成学生对象,写到本地文件</p>
<p>实现步骤：</p>
<ul>
<li>创建Properties集合,将本地文件中的数据加载到集合中</li>
<li>获取集合中的键值对数据,封装到学生对象中</li>
<li>创建序列化流对象,将学生对象序列化到本地文件中</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Serializable</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">long</span> <span class="variable">serialVersionUID</span> <span class="operator">=</span> <span class="number">1L</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">      	<span class="comment">//1.创建Properties集合,将本地文件中的数据加载到集合中</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">        <span class="type">FileReader</span> <span class="variable">fr</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileReader</span>(<span class="string">&quot;prop.properties&quot;</span>);</span><br><span class="line">        prop.load(fr);</span><br><span class="line">        fr.close();</span><br><span class="line">		<span class="comment">//2.获取集合中的键值对数据,封装到学生对象中</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> prop.getProperty(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> Integer.parseInt(prop.getProperty(<span class="string">&quot;age&quot;</span>));</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(name,age);</span><br><span class="line">		<span class="comment">//3.创建序列化流对象,将学生对象序列化到本地文件中</span></span><br><span class="line">        <span class="type">ObjectOutputStream</span> <span class="variable">oos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectOutputStream</span>(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;a.txt&quot;</span>));</span><br><span class="line">        oos.writeObject(s);</span><br><span class="line">        oos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>



</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/02/16/4.%E9%9B%86%E5%90%88/">4.集合</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-16</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-09-14</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-Collection集合"   >
          <a href="#1-Collection集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-Collection集合" class="headerlink" title="1.Collection集合"></a>1.Collection集合</h1>
      
        <h2 id="1-1数组和集合的区别"   >
          <a href="#1-1数组和集合的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1数组和集合的区别" class="headerlink" title="1.1数组和集合的区别"></a>1.1数组和集合的区别</h2>
      <ul>
<li><p>相同点：都是容器,可以存储多个数据</p>
</li>
<li><p>不同点：</p>
<ol>
<li>数组的长度是不可变的,集合的长度是可变的</li>
<li>数组可以存基本数据类型和引用数据类型</li>
<li>集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类</li>
</ol>
</li>
</ul>

        <h2 id="1-2-集合类体系结构"   >
          <a href="#1-2-集合类体系结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-集合类体系结构" class="headerlink" title="1.2 集合类体系结构"></a>1.2 集合类体系结构</h2>
      <p><img src="https://s2.loli.net/2022/02/16/kpsH1biCDzERfZo.png" alt="01_集合类体系结构图"></p>

        <h2 id="1-3-概述和使用"   >
          <a href="#1-3-概述和使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-概述和使用" class="headerlink" title="1.3 概述和使用"></a>1.3 概述和使用</h2>
      
        <h3 id="1-3-1-Collection集合概述"   >
          <a href="#1-3-1-Collection集合概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-1-Collection集合概述" class="headerlink" title="1.3.1 Collection集合概述"></a>1.3.1 Collection集合概述</h3>
      <ul>
<li>是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素</li>
<li>JDK 不提供此接口的任何直接实现.它提供更具体的子接口(如Set和List)实现</li>
</ul>

        <h3 id="1-3-2-创建Collection集合的对象"   >
          <a href="#1-3-2-创建Collection集合的对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-2-创建Collection集合的对象" class="headerlink" title="1.3.2 创建Collection集合的对象"></a>1.3.2 创建Collection集合的对象</h3>
      <ul>
<li>多态的方式</li>
<li>具体的实现类ArrayList</li>
</ul>

        <h3 id="1-3-3-Collection集合常用方法"   >
          <a href="#1-3-3-Collection集合常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-3-Collection集合常用方法" class="headerlink" title="1.3.3 Collection集合常用方法"></a>1.3.3 Collection集合常用方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">add</span><span class="params">(E e)</span>	<span class="comment">//添加元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">remove</span><span class="params">(Object o)</span>	<span class="comment">//从集合中移除指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">removeIf</span><span class="params">(Object o)</span>	<span class="comment">//根据条件进行移除</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span>   <span class="title function_">clear</span><span class="params">()</span>	<span class="comment">//清空集合中的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">contains</span><span class="params">(Object o)</span>	<span class="comment">//判断集合中是否存在指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span>	<span class="comment">//判断集合是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="type">int</span>   <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//集合的长度，也就是集合中元素的个数</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="1-4-Collection集合的遍历"   >
          <a href="#1-4-Collection集合的遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-Collection集合的遍历" class="headerlink" title="1.4 Collection集合的遍历"></a>1.4 Collection集合的遍历</h2>
      
        <h3 id="1-4-1-迭代器"   >
          <a href="#1-4-1-迭代器" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-迭代器" class="headerlink" title="1.4.1 迭代器"></a>1.4.1 迭代器</h3>
      
        <h4 id="1-4-1-1-迭代器介绍"   >
          <a href="#1-4-1-1-迭代器介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-1-迭代器介绍" class="headerlink" title="1.4.1.1 迭代器介绍"></a>1.4.1.1 迭代器介绍</h4>
      <ul>
<li>迭代器,集合的专用遍历方式</li>
<li><code>Iterator&lt;E&gt; iterator()</code>: 返回此集合中元素的迭代器,通过集合对象的<code>iterator()方法</code>得到</li>
</ul>

        <h4 id="1-4-1-2-Iterator中的常用方法"   >
          <a href="#1-4-1-2-Iterator中的常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-2-Iterator中的常用方法" class="headerlink" title="1.4.1.2 Iterator中的常用方法"></a>1.4.1.2 Iterator中的常用方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="title function_">hasNext</span><span class="params">()</span>	<span class="comment">//判断当前位置是否有元素可以被取出</span></span><br><span class="line">E <span class="title function_">next</span><span class="params">()</span>	<span class="comment">//获取当前位置的元素,将迭代器对象移向下一个索引位置</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="1-4-1-3-Collection集合的遍历"   >
          <a href="#1-4-1-3-Collection集合的遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-3-Collection集合的遍历" class="headerlink" title="1.4.1.3 Collection集合的遍历"></a>1.4.1.3 Collection集合的遍历</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IteratorDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Collection&lt;String&gt; c = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        c.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        c.add(<span class="string">&quot;javaee&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Iterator&lt;E&gt; iterator()：返回此集合中元素的迭代器，通过集合的iterator()方法得到</span></span><br><span class="line">        Iterator&lt;String&gt; it = c.iterator();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用while循环改进元素的判断和获取</span></span><br><span class="line">        <span class="keyword">while</span> (it.hasNext()) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="1-4-1-4-迭代器中删除的方法"   >
          <a href="#1-4-1-4-迭代器中删除的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-1-4-迭代器中删除的方法" class="headerlink" title="1.4.1.4 迭代器中删除的方法"></a>1.4.1.4 迭代器中删除的方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">remove</span><span class="params">()</span>	<span class="comment">//删除迭代器对象当前指向的元素</span></span><br></pre></td></tr></table></div></figure>

<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IteratorDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;c&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;d&quot;</span>);</span><br><span class="line"></span><br><span class="line">        Iterator&lt;String&gt; it = list.iterator();</span><br><span class="line">        <span class="keyword">while</span>(it.hasNext())&#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            <span class="keyword">if</span>(<span class="string">&quot;b&quot;</span>.equals(s))&#123;</span><br><span class="line">                <span class="comment">//指向谁,那么此时就删除谁.</span></span><br><span class="line">                it.remove();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(list);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="1-4-2-增强for循环"   >
          <a href="#1-4-2-增强for循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-2-增强for循环" class="headerlink" title="1.4.2 增强for循环"></a>1.4.2 增强for循环</h3>
      <p>介绍：</p>
<ul>
<li>它是JDK5之后出现的,其内部原理是一个Iterator迭代器</li>
<li>实现Iterable接口的类才可以使用迭代器和增强for</li>
<li>简化数组和Collection集合的遍历</li>
</ul>
<p>格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(集合/数组中元素的数据类型 变量名 :  集合/数组名) &#123;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 已经将当前遍历到的元素封装到变量中了,直接使用变量即可</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyCollectonDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list =  <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;b&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;c&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;d&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;e&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;f&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//1,数据类型一定是集合或者数组中元素的类型</span></span><br><span class="line">        <span class="comment">//2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素</span></span><br><span class="line">        <span class="comment">//3,list就是要遍历的集合或者数组</span></span><br><span class="line">        <span class="keyword">for</span>(String str : list)&#123;</span><br><span class="line">            System.out.println(str);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="2-泛型"   >
          <a href="#2-泛型" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-泛型" class="headerlink" title="2.泛型"></a>2.泛型</h1>
      
        <h2 id="2-1-泛型概述"   >
          <a href="#2-1-泛型概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-泛型概述" class="headerlink" title="2.1 泛型概述"></a>2.1 泛型概述</h2>
      <p>泛型的介绍：泛型是JDK5中引入的特性，它提供了编译时类型安全检测机制</p>
<p>泛型的好处：</p>
<ol>
<li>把运行时期的问题提前到了编译期间</li>
<li>避免了强制类型转换</li>
</ol>
<p>泛型的定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;类型&gt;: 指定一种类型的格式.尖括号里面可以任意书写,一般只写一个字母.例如: &lt;E&gt; &lt;T&gt;</span><br><span class="line">&lt;类型<span class="number">1</span>,类型<span class="number">2</span>…&gt;: 指定多种类型的格式,多种类型之间用逗号隔开.例如: &lt;E,T&gt; &lt;K,V&gt; </span><br></pre></td></tr></table></div></figure>


        <h2 id="2-2-泛型类"   >
          <a href="#2-2-泛型类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-泛型类" class="headerlink" title="2.2 泛型类"></a>2.2 泛型类</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 class 类名&lt;类型&gt; &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>泛型类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">private</span> T t;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> T <span class="title function_">getT</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> t;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setT</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.t = t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        Generic&lt;String&gt; g1 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;String&gt;();</span><br><span class="line">        g1.setT(<span class="string">&quot;杨幂&quot;</span>);</span><br><span class="line">        System.out.println(g1.getT());</span><br><span class="line"></span><br><span class="line">        Generic&lt;Integer&gt; g2 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;Integer&gt;();</span><br><span class="line">        g2.setT(<span class="number">30</span>);</span><br><span class="line">        System.out.println(g2.getT());</span><br><span class="line"></span><br><span class="line">        Generic&lt;Boolean&gt; g3 = <span class="keyword">new</span> <span class="title class_">Generic</span>&lt;Boolean&gt;();</span><br><span class="line">        g3.setT(<span class="literal">true</span>);</span><br><span class="line">        System.out.println(g3.getT());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-3-泛型方法"   >
          <a href="#2-3-泛型方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-泛型方法" class="headerlink" title="2.3 泛型方法"></a>2.3 泛型方法</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 &lt;类型&gt; 返回值类型 方法名(类型 变量名) &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>带有泛型方法的类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Generic</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">	    <span class="type">Generic</span> <span class="variable">g</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Generic</span>();</span><br><span class="line">        g.show(<span class="string">&quot;柳岩&quot;</span>);</span><br><span class="line">        g.show(<span class="number">30</span>);</span><br><span class="line">        g.show(<span class="literal">true</span>);</span><br><span class="line">        g.show(<span class="number">12.34</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-4-泛型接口"   >
          <a href="#2-4-泛型接口" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-泛型接口" class="headerlink" title="2.4 泛型接口"></a>2.4 泛型接口</h2>
      <p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 interface 接口名&lt;类型&gt; &#123;  &#125;</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<ul>
<li><p>泛型接口</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>泛型接口实现类1</p>
<p>​	定义实现类时,定义和接口相同泛型,创建实现类对象时明确泛型的具体类型</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericImpl1</span>&lt;T&gt; <span class="keyword">implements</span> <span class="title class_">Generic</span>&lt;T&gt; &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(T t)</span> &#123;</span><br><span class="line">        System.out.println(t);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>泛型接口实现类2</p>
<p>​	定义实现类时,直接明确泛型的具体类型</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericImpl2</span> <span class="keyword">implements</span> <span class="title class_">Generic</span>&lt;Integer&gt;&#123;</span><br><span class="line">     <span class="meta">@Override</span></span><br><span class="line">     <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">(Integer t)</span> &#123;</span><br><span class="line">          System.out.println(t);</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        GenericImpl1&lt;String&gt; g1 = <span class="keyword">new</span> <span class="title class_">GenericImpl</span>&lt;String&gt;();</span><br><span class="line">        g1.show(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        GenericImpl1&lt;Integer&gt; g2 = <span class="keyword">new</span> <span class="title class_">GenericImpl</span>&lt;Integer&gt;();</span><br><span class="line">        g2.show(<span class="number">30</span>);</span><br><span class="line">      </span><br><span class="line">        <span class="type">GenericImpl2</span> <span class="variable">g3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GenericImpl2</span>();</span><br><span class="line">      	g3.show(<span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="2-5-类型通配符"   >
          <a href="#2-5-类型通配符" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-5-类型通配符" class="headerlink" title="2.5 类型通配符"></a>2.5 类型通配符</h2>
      <ul>
<li><p>类型通配符: <code>&lt;?&gt;</code></p>
<ul>
<li><code>ArrayList&lt;?&gt;</code>: 表示元素类型未知的<code>ArrayList</code>,它的元素可以匹配任何的类型</li>
<li>但是并不能把元素添加到<code>ArrayList</code>中了,获取出来的也是父类类型</li>
</ul>
</li>
<li><p>类型通配符上限: <code>&lt;? extends 类型&gt;</code></p>
<ul>
<li><code>ArrayListList &lt;? extends Number&gt;</code>: 它表示的类型是Number或者其子类型</li>
</ul>
</li>
<li><p>类型通配符下限: <code>&lt;? super 类型&gt;</code></p>
<ul>
<li><code>ArrayListList &lt;? super Number&gt;</code>: 它表示的类型是Number或者其父类型</li>
</ul>
</li>
<li><p>泛型通配符的使用</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenericDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;Integer&gt; list1 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;String&gt; list2 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;Number&gt; list3 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        ArrayList&lt;Object&gt; list4 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        method(list1);</span><br><span class="line">        method(list2);</span><br><span class="line">        method(list3);</span><br><span class="line">        method(list4);</span><br><span class="line"></span><br><span class="line">        getElement1(list1);</span><br><span class="line">        getElement1(list2);<span class="comment">//报错</span></span><br><span class="line">        getElement1(list3);</span><br><span class="line">        getElement1(list4);<span class="comment">//报错</span></span><br><span class="line"></span><br><span class="line">        getElement2(list1);<span class="comment">//报错</span></span><br><span class="line">        getElement2(list2);<span class="comment">//报错</span></span><br><span class="line">        getElement2(list3);</span><br><span class="line">        getElement2(list4);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型通配符: 此时的泛型?,可以是任意类型</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">(ArrayList&lt;?&gt; list)</span>&#123;&#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型的上限: 此时的泛型?,必须是Number类型或者Number类型的子类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getElement1</span><span class="params">(ArrayList&lt;? extends Number&gt; list)</span>&#123;&#125;</span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 泛型的下限: 此时的泛型?,必须是Number类型或者Number类型的父类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getElement2</span><span class="params">(ArrayList&lt;? <span class="built_in">super</span> Number&gt; list)</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h1 id="3-List集合"   >
          <a href="#3-List集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-List集合" class="headerlink" title="3.List集合"></a>3.List集合</h1>
      
        <h2 id="3-1-概述和特点"   >
          <a href="#3-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-概述和特点" class="headerlink" title="3.1 概述和特点"></a>3.1 概述和特点</h2>
      <p>概述：</p>
<ul>
<li>有序集合,这里的有序指的是存取顺序</li>
<li>用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素</li>
<li>与Set集合不同,列表通常允许重复的元素</li>
</ul>
<p>特点：</p>
<ul>
<li>存取有序</li>
<li>可以重复</li>
<li>有索引</li>
</ul>

        <h2 id="3-2-List集合的特有方法"   >
          <a href="#3-2-List集合的特有方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-List集合的特有方法" class="headerlink" title="3.2 List集合的特有方法"></a>3.2 List集合的特有方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//在此集合中的指定位置插入指定的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">remove</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//删除指定索引处的元素，返回被删除的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">set</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//修改指定索引处的元素，返回被修改的元素</span></span><br><span class="line">  </span><br><span class="line">E <span class="title function_">get</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//返回指定索引处的元素</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="3-1-List集合的实现类"   >
          <a href="#3-1-List集合的实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-List集合的实现类" class="headerlink" title="3.1 List集合的实现类"></a>3.1 List集合的实现类</h2>
      
        <h3 id="3-1-1-List集合子类的特点"   >
          <a href="#3-1-1-List集合子类的特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-1-List集合子类的特点" class="headerlink" title="3.1.1 List集合子类的特点"></a>3.1.1 List集合子类的特点</h3>
      <ul>
<li><p>ArrayList集合</p>
<p>​	底层是数组结构实现，查询快、增删慢</p>
</li>
<li><p>LinkedList集合</p>
<p>​	底层是链表结构实现，查询慢、增删快</p>
</li>
</ul>

        <h3 id="3-1-2-ArrayList"   >
          <a href="#3-1-2-ArrayList" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-ArrayList" class="headerlink" title="3.1.2 ArrayList"></a>3.1.2 ArrayList</h3>
      
        <h4 id="3-1-2-1-构造方法和添加方法"   >
          <a href="#3-1-2-1-构造方法和添加方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-1-构造方法和添加方法" class="headerlink" title="3.1.2.1 构造方法和添加方法"></a>3.1.2.1 构造方法和添加方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">ArrayList</span><span class="params">()</span>	<span class="comment">//创建一个空的集合对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">add</span><span class="params">(E e)</span>		<span class="comment">//将指定的元素追加到此集合的末尾</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//在此集合中的指定位置插入指定的元素</span></span><br></pre></td></tr></table></div></figure>

<p><code>ArrayList&lt;E&gt;</code>  ：</p>
<p>​	可调整大小的数组实现 </p>
<p>​	<code>&lt;E&gt;</code> : 是一种特殊的数据类型，泛型。</p>
<p>怎么用呢 ?</p>
<p>​	在出现E的地方我们使用引用数据类型替换即可	</p>
<p>​	举例：<code>ArrayList&lt;String&gt;</code>, <code>ArrayList&lt;Student&gt;</code></p>

        <h4 id="3-1-2-2-常用方法"   >
          <a href="#3-1-2-2-常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-2-常用方法" class="headerlink" title="3.1.2.2 常用方法"></a>3.1.2.2 常用方法</h4>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">remove</span><span class="params">(Object o)</span>	<span class="comment">//删除指定的元素，返回删除是否成功</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">remove</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//删除指定索引处的元素，返回被删除的元素</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">set</span><span class="params">(<span class="type">int</span> index,E element)</span>	<span class="comment">//修改指定索引处的元素，返回被修改的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">get</span><span class="params">(<span class="type">int</span> index)</span>	<span class="comment">//返回指定索引处的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//返回集合中的元素的个数</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-3-LinkedList"   >
          <a href="#3-1-3-LinkedList" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-3-LinkedList" class="headerlink" title="3.1.3 LinkedList"></a>3.1.3 LinkedList</h3>
      <p>特有方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addFirst</span><span class="params">(E e)</span>	<span class="comment">//在该列表开头插入指定的元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addLast</span><span class="params">(E e)</span>	<span class="comment">//将指定的元素追加到此列表的末尾</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">getFirst</span><span class="params">()</span>	<span class="comment">//返回此列表中的第一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">getLast</span><span class="params">()</span>	<span class="comment">//返回此列表中的最后一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">removeFirst</span><span class="params">()</span>	<span class="comment">//从此列表中删除并返回第一个元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> E <span class="title function_">removeLast</span><span class="params">()</span>	<span class="comment">//从此列表中删除并返回最后一个元素</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="4-Set集合"   >
          <a href="#4-Set集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-Set集合" class="headerlink" title="4.Set集合"></a>4.Set集合</h1>
      
        <h2 id="4-1-概述和特点"   >
          <a href="#4-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-1-概述和特点" class="headerlink" title="4.1 概述和特点"></a>4.1 概述和特点</h2>
      <ul>
<li>不可以存储重复元素</li>
<li>没有索引,不能使用普通for循环遍历</li>
</ul>

        <h2 id="4-2-Set集合的使用"   >
          <a href="#4-2-Set集合的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-Set集合的使用" class="headerlink" title="4.2 Set集合的使用"></a>4.2 Set集合的使用</h2>
      <p>存储字符串并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MySet1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">//创建集合对象</span></span><br><span class="line">        Set&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;();</span><br><span class="line">      	<span class="comment">//添加元素</span></span><br><span class="line">        set.add(<span class="string">&quot;ccc&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;bbb&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; set.size(); i++) &#123;</span></span><br><span class="line"><span class="comment">//            //Set集合是没有索引的，所以不能使用通过索引获取元素的方法</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line">      </span><br><span class="line">      	<span class="comment">//遍历集合</span></span><br><span class="line">        Iterator&lt;String&gt; it = set.iterator();</span><br><span class="line">        <span class="keyword">while</span> (it.hasNext())&#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> it.next();</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;-----------------------------------&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (String s : set) &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="4-3-Set集合实现类"   >
          <a href="#4-3-Set集合实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-Set集合实现类" class="headerlink" title="4.3 Set集合实现类"></a>4.3 Set集合实现类</h2>
      
        <h3 id="4-3-1-TreeSet"   >
          <a href="#4-3-1-TreeSet" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-TreeSet" class="headerlink" title="4.3.1 TreeSet"></a>4.3.1 TreeSet</h3>
      
        <h4 id="4-3-1-1概述和特点"   >
          <a href="#4-3-1-1概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-1概述和特点" class="headerlink" title="4.3.1.1概述和特点"></a>4.3.1.1概述和特点</h4>
      <ul>
<li>不可以存储重复元素</li>
<li>没有索引</li>
<li>可以将元素按照规则进行排序<ul>
<li><code>TreeSet()</code>：根据其元素的自然排序进行排序</li>
<li><code>TreeSet(Comparator comparator)</code> ：根据指定的比较器进行排序</li>
</ul>
</li>
</ul>

        <h4 id="4-3-1-2-集合基本使用"   >
          <a href="#4-3-1-2-集合基本使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-2-集合基本使用" class="headerlink" title="4.3.1.2 集合基本使用"></a>4.3.1.2 集合基本使用</h4>
      <p>存储Integer类型的整数并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TreeSetDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Integer&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;Integer&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        ts.add(<span class="number">10</span>);</span><br><span class="line">        ts.add(<span class="number">40</span>);</span><br><span class="line">        ts.add(<span class="number">30</span>);</span><br><span class="line">        ts.add(<span class="number">50</span>);</span><br><span class="line">        ts.add(<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">        ts.add(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span>(Integer i : ts) &#123;</span><br><span class="line">            System.out.println(i);</span><br><span class="line">        &#125;</span><br><span class="line">      <span class="comment">//10	20	30	40	50</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-3-自然排序Comparable的使用"   >
          <a href="#4-3-1-3-自然排序Comparable的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-3-自然排序Comparable的使用" class="headerlink" title="4.3.1.3 自然排序Comparable的使用"></a>4.3.1.3 自然排序Comparable的使用</h4>
      <p>案例需求:</p>
<ol>
<li>存储学生对象并遍历，创建TreeSet集合使用无参构造方法</li>
<li>要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序</li>
</ol>
<p>实现步骤:</p>
<ol>
<li>使用空参构造创建TreeSet集合<ul>
<li>用TreeSet集合存储自定义对象，无参构造方法使用的是自然排序对元素进行排序的</li>
</ul>
</li>
<li>自定义的Student类实现Comparable接口<ul>
<li>自然排序，就是让元素所属的类实现Comparable接口，重写compareTo(T o)方法</li>
</ul>
</li>
<li>重写接口中的compareTo方法<ul>
<li>重写方法时，一定要注意排序规则必须按照要求的主要条件和次要条件来写</li>
</ul>
</li>
</ol>
<p>代码实现:</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compareTo</span><span class="params">(Student o)</span> &#123;</span><br><span class="line">        <span class="comment">//按照对象的年龄进行排序</span></span><br><span class="line">        <span class="comment">//主要判断条件: 按照年龄从小到大排序</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> <span class="built_in">this</span>.age - o.age;</span><br><span class="line">        <span class="comment">//次要判断条件: 年龄相同时，按照姓名的字母顺序排序</span></span><br><span class="line">        result = result == <span class="number">0</span> ? <span class="built_in">this</span>.name.compareTo(o.getName()) : result;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyTreeSet2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Student&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;();</span><br><span class="line">	    <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;zhangsan&quot;</span>,<span class="number">28</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;lisi&quot;</span>,<span class="number">27</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;wangwu&quot;</span>,<span class="number">29</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;zhaoliu&quot;</span>,<span class="number">28</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s5</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;qianqi&quot;</span>,<span class="number">30</span>);</span><br><span class="line">		<span class="comment">//把学生添加到集合</span></span><br><span class="line">        ts.add(s1);</span><br><span class="line">        ts.add(s2);</span><br><span class="line">        ts.add(s3);</span><br><span class="line">        ts.add(s4);</span><br><span class="line">        ts.add(s5);</span><br><span class="line">		<span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span> (Student student : ts) &#123;</span><br><span class="line">            System.out.println(student);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-4-比较器排序Comparator的使用"   >
          <a href="#4-3-1-4-比较器排序Comparator的使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-4-比较器排序Comparator的使用" class="headerlink" title="4.3.1.4 比较器排序Comparator的使用"></a>4.3.1.4 比较器排序Comparator的使用</h4>
      <p>案例需求:</p>
<ol>
<li>存储老师对象并遍历，创建TreeSet集合使用带参构造方法</li>
<li>要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序</li>
</ol>
<p>实现步骤:</p>
<ol>
<li>用TreeSet集合存储自定义对象，带参构造方法使用的是比较器排序对元素进行排序的</li>
<li>比较器排序，就是让集合构造方法接收Comparator的实现类对象，重写compare(T o1,T o2)方法</li>
<li>重写方法时，一定要注意排序规则必须按照要求的主要条件和次要条件来写</li>
</ol>
<p>代码实现:</p>
<p>老师类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Teacher</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Teacher</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Teacher</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Teacher&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyTreeSet4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">//创建集合对象</span></span><br><span class="line">        TreeSet&lt;Teacher&gt; ts = <span class="keyword">new</span> <span class="title class_">TreeSet</span>&lt;&gt;(<span class="keyword">new</span> <span class="title class_">Comparator</span>&lt;Teacher&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compare</span><span class="params">(Teacher o1, Teacher o2)</span> &#123;</span><br><span class="line">                <span class="comment">//o1表示现在要存入的那个元素</span></span><br><span class="line">                <span class="comment">//o2表示已经存入到集合中的元素</span></span><br><span class="line">              </span><br><span class="line">                <span class="comment">//主要条件</span></span><br><span class="line">                <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> o1.getAge() - o2.getAge();</span><br><span class="line">                <span class="comment">//次要条件</span></span><br><span class="line">                result = result == <span class="number">0</span> ? o1.getName().compareTo(o2.getName()) : result;</span><br><span class="line">                <span class="keyword">return</span> result;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">		<span class="comment">//创建老师对象</span></span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;zhangsan&quot;</span>,<span class="number">23</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;lisi&quot;</span>,<span class="number">22</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;wangwu&quot;</span>,<span class="number">24</span>);</span><br><span class="line">        <span class="type">Teacher</span> <span class="variable">t4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;zhaoliu&quot;</span>,<span class="number">24</span>);</span><br><span class="line">		<span class="comment">//把老师添加到集合</span></span><br><span class="line">        ts.add(t1);</span><br><span class="line">        ts.add(t2);</span><br><span class="line">        ts.add(t3);</span><br><span class="line">        ts.add(t4);</span><br><span class="line">		<span class="comment">//遍历集合</span></span><br><span class="line">        <span class="keyword">for</span> (Teacher teacher : ts) &#123;</span><br><span class="line">            System.out.println(teacher);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-1-5-两种比较方式总结"   >
          <a href="#4-3-1-5-两种比较方式总结" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-1-5-两种比较方式总结" class="headerlink" title="4.3.1.5 两种比较方式总结"></a>4.3.1.5 两种比较方式总结</h4>
      <ul>
<li>两种比较方式小结<ul>
<li>自然排序: 自定义类实现Comparable接口,重写compareTo方法,根据返回值进行排序</li>
<li>比较器排序: 创建TreeSet对象的时候传递Comparator的实现类对象,重写compare方法,根据返回值进行排序</li>
<li>在使用的时候,默认使用自然排序,当自然排序不满足现在的需求时,必须使用比较器排序</li>
</ul>
</li>
<li>两种方式中关于返回值的规则<ul>
<li>如果返回值为负数，表示当前存入的元素是较小值，存左边</li>
<li>如果返回值为0，表示当前存入的元素跟集合中元素重复了，不存</li>
<li>如果返回值为正数，表示当前存入的元素是较大值，存右边</li>
</ul>
</li>
</ul>

        <h3 id="4-3-2-HashSet"   >
          <a href="#4-3-2-HashSet" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-HashSet" class="headerlink" title="4.3.2 HashSet"></a>4.3.2 HashSet</h3>
      
        <h4 id="4-3-2-1-概述和特点"   >
          <a href="#4-3-2-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-1-概述和特点" class="headerlink" title="4.3.2.1 概述和特点"></a>4.3.2.1 概述和特点</h4>
      <ul>
<li>底层数据结构是哈希表</li>
<li>存取无序</li>
<li>不可以存储重复元素</li>
<li>没有索引,不能使用普通for循环遍历</li>
</ul>

        <h4 id="4-3-2-2-基本应用"   >
          <a href="#4-3-2-2-基本应用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-2-基本应用" class="headerlink" title="4.3.2.2 基本应用"></a>4.3.2.2 基本应用</h4>
      <p>存储字符串并遍历</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashSetDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        HashSet&lt;String&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        set.add(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        set.add(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        <span class="comment">//不包含重复元素的集合</span></span><br><span class="line">        set.add(<span class="string">&quot;world&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历</span></span><br><span class="line">        <span class="keyword">for</span>(String s : set) &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="4-3-2-3-哈希值"   >
          <a href="#4-3-2-3-哈希值" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-3-哈希值" class="headerlink" title="4.3.2.3 哈希值"></a>4.3.2.3 哈希值</h4>
      <p>哈希值简介：</p>
<p>​	是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值</p>
<p>如何获取哈希值：</p>
<p>​	<code>Object</code>类中的<code>public int hashCode()</code>：返回对象的哈希码值</p>
<p>哈希值的特点：</p>
<ul>
<li>同一个对象多次调用hashCode()方法返回的哈希值是相同的</li>
<li>默认情况下，不同对象的哈希值是不同的。而重写hashCode()方法，可以实现让不同对象的哈希值相同</li>
</ul>

        <h4 id="4-3-2-4-哈希表结构"   >
          <a href="#4-3-2-4-哈希表结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-4-哈希表结构" class="headerlink" title="4.3.2.4 哈希表结构"></a>4.3.2.4 哈希表结构</h4>
      <p>JDK1.8以前</p>
<p>​	数组 + 链表</p>
<p><img src="https://s2.loli.net/2022/02/16/ohlkCrNfOveXpLD.png" alt="14_JKD8以前哈希表"> JDK1.8以后</p>
<ul>
<li><p>节点个数少于等于8个</p>
<p>​	数组 + 链表</p>
</li>
<li><p>节点个数多于8个</p>
<p>​	数组 + 红黑树</p>
</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/16/ECLbPKZOpUGig8c.png" alt="15_JKD8以后哈希表"></p>

        <h4 id="4-3-2-5-HashSet集合存储学生对象并遍历"   >
          <a href="#4-3-2-5-HashSet集合存储学生对象并遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-3-2-5-HashSet集合存储学生对象并遍历" class="headerlink" title="4.3.2.5 HashSet集合存储学生对象并遍历"></a>4.3.2.5 HashSet集合存储学生对象并遍历</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个存储学生对象的集合，存储多个学生对象，使用程序实现在控制台遍历该集合</li>
<li>要求：学生对象的成员变量值相同，我们就认为是同一个对象</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> name != <span class="literal">null</span> ? name.hashCode() : <span class="number">0</span>;</span><br><span class="line">        result = <span class="number">31</span> * result + age;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashSetDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建HashSet集合对象</span></span><br><span class="line">        HashSet&lt;Student&gt; hs = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;Student&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;林青霞&quot;</span>, <span class="number">30</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张曼玉&quot;</span>, <span class="number">35</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把学生添加到集合</span></span><br><span class="line">        hs.add(s1);</span><br><span class="line">        hs.add(s2);</span><br><span class="line">        hs.add(s3);</span><br><span class="line">        hs.add(s4);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合(增强for)</span></span><br><span class="line">        <span class="keyword">for</span> (Student s : hs) &#123;</span><br><span class="line">            System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>总结：	HashSet集合存储自定义类型元素,要想实现元素的唯一,要求必须重写hashCode方法和equals方法</p>

        <h1 id="5-Map集合"   >
          <a href="#5-Map集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-Map集合" class="headerlink" title="5.Map集合"></a>5.Map集合</h1>
      
        <h2 id="5-1-概述和特点"   >
          <a href="#5-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-概述和特点" class="headerlink" title="5.1 概述和特点"></a>5.1 概述和特点</h2>
      <p>Map集合概述</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Map</span>&lt;K,V&gt;  K：键的类型；V：值的类型</span><br></pre></td></tr></table></div></figure>

<p>Map集合的特点</p>
<ul>
<li>双列集合,一个键对应一个值</li>
<li>键不可以重复,值可以重复</li>
</ul>

        <h2 id="5-2-基本方法"   >
          <a href="#5-2-基本方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-2-基本方法" class="headerlink" title="5.2 基本方法"></a>5.2 基本方法</h2>
      <p>方法介绍</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">V <span class="title function_">put</span><span class="params">(K key,V value)</span>	<span class="comment">//添加元素</span></span><br><span class="line">  </span><br><span class="line">V <span class="title function_">remove</span><span class="params">(Object key)</span>	<span class="comment">//根据键删除键值对元素</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">void</span> <span class="title function_">clear</span><span class="params">()</span>	<span class="comment">//移除所有的键值对元素</span></span><br><span class="line"></span><br><span class="line"><span class="type">boolean</span> <span class="title function_">containsKey</span><span class="params">(Object key)</span>	<span class="comment">//判断集合是否包含指定的键</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">containsValue</span><span class="params">(Object value)</span>	<span class="comment">//判断集合是否包含指定的值</span></span><br><span class="line">  </span><br><span class="line"><span class="type">boolean</span> <span class="title function_">isEmpty</span><span class="params">()</span>	<span class="comment">//判断集合是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="type">int</span> <span class="title function_">size</span><span class="params">()</span>	<span class="comment">//集合的长度，也就是集合中键值对的个数</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="5-3-获取方法"   >
          <a href="#5-3-获取方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-3-获取方法" class="headerlink" title="5.3 获取方法"></a>5.3 获取方法</h2>
      <p>方法介绍</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">V <span class="title function_">get</span><span class="params">(Object key)</span>	<span class="comment">//根据键获取值</span></span><br><span class="line">  </span><br><span class="line">Set&lt;K&gt; <span class="title function_">keySet</span><span class="params">()</span>	<span class="comment">//获取所有键的集合</span></span><br><span class="line">  </span><br><span class="line">Collection&lt;V&gt; <span class="title function_">values</span><span class="params">()</span>	<span class="comment">//获取所有值的集合</span></span><br><span class="line">  </span><br><span class="line">Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()	<span class="comment">//获取所有键值对对象的集合</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="5-4-keySet-方式遍历"   >
          <a href="#5-4-keySet-方式遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-4-keySet-方式遍历" class="headerlink" title="5.4 keySet()方式遍历"></a>5.4 <code>keySet()</code>方式遍历</h2>
      <p>遍历思路：我们刚才存储的元素都是成对出现的，所以我们把Map看成是一个夫妻对的集合</p>
<ul>
<li>把所有的丈夫给集中起来</li>
<li>遍历丈夫的集合，获取到每一个丈夫</li>
<li>根据丈夫去找对应的妻子</li>
</ul>
<p>步骤分析：</p>
<ul>
<li>获取所有键的集合。用keySet()方法实现</li>
<li>遍历键的集合，获取到每一个键。用增强for实现  </li>
<li>根据键去找值。用get(Object key)方法实现</li>
</ul>
<p>代码实现</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MapDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Map&lt;String, String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        map.put(<span class="string">&quot;张无忌&quot;</span>, <span class="string">&quot;赵敏&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;郭靖&quot;</span>, <span class="string">&quot;黄蓉&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;杨过&quot;</span>, <span class="string">&quot;小龙女&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取所有键的集合。用keySet()方法实现</span></span><br><span class="line">        Set&lt;String&gt; keySet = map.keySet();</span><br><span class="line">        <span class="comment">//遍历键的集合，获取到每一个键。用增强for实现</span></span><br><span class="line">        <span class="keyword">for</span> (String key : keySet) &#123;</span><br><span class="line">            <span class="comment">//根据键去找值。用get(Object key)方法实现</span></span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> map.get(key);</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-5-entrySet-方式遍历"   >
          <a href="#5-5-entrySet-方式遍历" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-5-entrySet-方式遍历" class="headerlink" title="5.5 entrySet()方式遍历"></a>5.5 <code>entrySet()</code>方式遍历</h2>
      <p>遍历思路：我们刚才存储的元素都是成对出现的，所以我们把Map看成是一个夫妻对的集合</p>
<ol>
<li>获取所有结婚证的集合</li>
<li>遍历结婚证的集合，得到每一个结婚证</li>
<li>根据结婚证获取丈夫和妻子</li>
</ol>
<p>步骤分析：</p>
<p>1.获取所有键值对对象的集合：<code>Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()</code></p>
<p>2.遍历键值对对象的集合，得到每一个键值对对象：用增强for实现，得到每一个<code>Map.Entry</code></p>
<p>3.根据键值对对象获取键和值：用<code>getKey()</code>得到键、用<code>getValue()</code>得到值</p>
<p>代码实现：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MapDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建集合对象</span></span><br><span class="line">        Map&lt;String, String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加元素</span></span><br><span class="line">        map.put(<span class="string">&quot;张无忌&quot;</span>, <span class="string">&quot;赵敏&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;郭靖&quot;</span>, <span class="string">&quot;黄蓉&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;杨过&quot;</span>, <span class="string">&quot;小龙女&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取所有键值对对象的集合</span></span><br><span class="line">        Set&lt;Map.Entry&lt;String, String&gt;&gt; entrySet = map.entrySet();</span><br><span class="line">        <span class="comment">//遍历键值对对象的集合，得到每一个键值对对象</span></span><br><span class="line">        <span class="keyword">for</span> (Map.Entry&lt;String, String&gt; me : entrySet) &#123;</span><br><span class="line">            <span class="comment">//根据键值对对象获取键和值</span></span><br><span class="line">            <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> me.getKey();</span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> me.getValue();</span><br><span class="line">            System.out.println(key + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="5-6-Map集合实现类"   >
          <a href="#5-6-Map集合实现类" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-Map集合实现类" class="headerlink" title="5.6 Map集合实现类"></a>5.6 Map集合实现类</h2>
      
        <h3 id="5-6-1-HashMap"   >
          <a href="#5-6-1-HashMap" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-HashMap" class="headerlink" title="5.6.1 HashMap"></a>5.6.1 HashMap</h3>
      
        <h4 id="5-6-1-1-概述和特点"   >
          <a href="#5-6-1-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-1-概述和特点" class="headerlink" title="5.6.1.1 概述和特点"></a>5.6.1.1 概述和特点</h4>
      <ul>
<li>HashMap底层是哈希表结构的</li>
<li>依赖<code>hashCode()</code>和<code>equals()</code>保证键的唯一</li>
<li>如果键要存储的是自定义对象，需要重写<code>hashCode()</code>和<code>equals()</code></li>
</ul>

        <h4 id="5-6-1-2-应用案例"   >
          <a href="#5-6-1-2-应用案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-1-2-应用案例" class="headerlink" title="5.6.1.2 应用案例"></a>5.6.1.2 应用案例</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个HashMap集合，键是学生对象(Student)，值是居住地 (String)。存储多个元素，并遍历。</li>
<li>要求保证键的唯一性：如果学生对象的成员变量值相同，我们就认为是同一个对象</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> name != <span class="literal">null</span> ? name.hashCode() : <span class="number">0</span>;</span><br><span class="line">        result = <span class="number">31</span> * result + age;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashMapDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建HashMap集合对象</span></span><br><span class="line">        HashMap&lt;Student, String&gt; hm = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;Student, String&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;林青霞&quot;</span>, <span class="number">30</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;张曼玉&quot;</span>, <span class="number">35</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;王祖贤&quot;</span>, <span class="number">33</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把学生添加到集合</span></span><br><span class="line">        hm.put(s1, <span class="string">&quot;西安&quot;</span>);</span><br><span class="line">        hm.put(s2, <span class="string">&quot;武汉&quot;</span>);</span><br><span class="line">        hm.put(s3, <span class="string">&quot;郑州&quot;</span>);</span><br><span class="line">        hm.put(s4, <span class="string">&quot;北京&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历集合</span></span><br><span class="line">        Set&lt;Student&gt; keySet = hm.keySet();</span><br><span class="line">        <span class="keyword">for</span> (Student key : keySet) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">value</span> <span class="operator">=</span> hm.get(key);</span><br><span class="line">            System.out.println(key.getName() + <span class="string">&quot;,&quot;</span> + key.getAge() + <span class="string">&quot;,&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="5-6-2-TreeMap"   >
          <a href="#5-6-2-TreeMap" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-TreeMap" class="headerlink" title="5.6.2 TreeMap"></a>5.6.2 TreeMap</h3>
      
        <h4 id="5-6-2-1-概述和特点"   >
          <a href="#5-6-2-1-概述和特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-1-概述和特点" class="headerlink" title="5.6.2.1 概述和特点"></a>5.6.2.1 概述和特点</h4>
      <ul>
<li>TreeMap底层是红黑树结构</li>
<li>依赖自然排序或者比较器排序,对键进行排序</li>
<li>如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap对象时候给出比较器排序规则</li>
</ul>

        <h4 id="5-6-2-2-应用案例"   >
          <a href="#5-6-2-2-应用案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-6-2-2-应用案例" class="headerlink" title="5.6.2.2 应用案例"></a>5.6.2.2 应用案例</h4>
      <p>案例需求：</p>
<ul>
<li>创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历</li>
<li>要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序</li>
</ul>
<p>代码实现：</p>
<p>学生类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">implements</span> <span class="title class_">Comparable</span>&lt;Student&gt;&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compareTo</span><span class="params">(Student o)</span> &#123;</span><br><span class="line">        <span class="comment">//按照年龄进行排序</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> o.getAge() - <span class="built_in">this</span>.getAge();</span><br><span class="line">        <span class="comment">//次要条件，按照姓名排序。</span></span><br><span class="line">        result = result == <span class="number">0</span> ? o.getName().compareTo(<span class="built_in">this</span>.getName()) : result;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>测试类</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      	<span class="comment">// 创建TreeMap集合对象</span></span><br><span class="line">        TreeMap&lt;Student,String&gt; tm = <span class="keyword">new</span> <span class="title class_">TreeMap</span>&lt;&gt;();</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 创建学生对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;xiaohei&quot;</span>,<span class="number">23</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;dapang&quot;</span>,<span class="number">22</span>);</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;xiaomei&quot;</span>,<span class="number">22</span>);</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 将学生对象添加到TreeMap集合中</span></span><br><span class="line">        tm.put(s1,<span class="string">&quot;江苏&quot;</span>);</span><br><span class="line">        tm.put(s2,<span class="string">&quot;北京&quot;</span>);</span><br><span class="line">        tm.put(s3,<span class="string">&quot;天津&quot;</span>);</span><br><span class="line">      </span><br><span class="line">		<span class="comment">// 遍历TreeMap集合,打印每个学生的信息</span></span><br><span class="line">        tm.forEach(</span><br><span class="line">                (Student key, String value)-&gt;&#123;</span><br><span class="line">                    System.out.println(key + <span class="string">&quot;---&quot;</span> + value);</span><br><span class="line">                &#125;</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="6-可变参数"   >
          <a href="#6-可变参数" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-可变参数" class="headerlink" title="6.可变参数"></a>6.可变参数</h1>
      
        <h2 id="6-1-可变参数"   >
          <a href="#6-1-可变参数" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-可变参数" class="headerlink" title="6.1 可变参数"></a>6.1 可变参数</h2>
      
        <h3 id="6-1-1-可变参数介绍"   >
          <a href="#6-1-1-可变参数介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-1-可变参数介绍" class="headerlink" title="6.1.1 可变参数介绍"></a>6.1.1 可变参数介绍</h3>
      <ul>
<li>可变参数又称参数个数可变，用作方法的形参出现，那么方法参数个数就是可变的了</li>
<li>方法的参数类型已经确定,个数不确定,我们可以使用可变参数</li>
</ul>

        <h3 id="6-1-2-可变参数定义格式"   >
          <a href="#6-1-2-可变参数定义格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-2-可变参数定义格式" class="headerlink" title="6.1.2 可变参数定义格式"></a>6.1.2 可变参数定义格式</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">修饰符 返回值类型 方法名(数据类型… 变量名) &#123;  &#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="6-1-3-可变参数的注意事项"   >
          <a href="#6-1-3-可变参数的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-3-可变参数的注意事项" class="headerlink" title="6.1.3 可变参数的注意事项"></a>6.1.3 可变参数的注意事项</h3>
      <ul>
<li>这里的变量其实是一个数组</li>
<li>如果一个方法有多个参数，包含可变参数，可变参数要放在最后</li>
</ul>

        <h3 id="6-1-4-可变参数的基本使用"   >
          <a href="#6-1-4-可变参数的基本使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-4-可变参数的基本使用" class="headerlink" title="6.1.4 可变参数的基本使用"></a>6.1.4 可变参数的基本使用</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArgsDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, <span class="number">40</span>));</span><br><span class="line"></span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>,<span class="number">70</span>));</span><br><span class="line">        System.out.println(sum(<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>,<span class="number">60</span>,<span class="number">70</span>,<span class="number">80</span>,<span class="number">90</span>,<span class="number">100</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//    public static int sum(int b,int... a) &#123;</span></span><br><span class="line"><span class="comment">//        return 0;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span>... a)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i : a) &#123;</span><br><span class="line">            sum += i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-2-创建不可变集合"   >
          <a href="#6-2-创建不可变集合" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-创建不可变集合" class="headerlink" title="6.2 创建不可变集合"></a>6.2 创建不可变集合</h2>
      <p>方法介绍：</p>
<ul>
<li>在List、Set、Map接口中,都存在of方法,可以创建一个不可变的集合<ul>
<li>这个集合不能添加,不能删除,不能修改</li>
<li>但是可以结合集合的带参构造,实现集合的批量添加</li>
</ul>
</li>
<li>在Map接口中,还有一个ofEntries方法可以提高代码的阅读性<ul>
<li>首先会把键值对封装成一个Entry对象,再把这个Entry对象添加到集合当中</li>
</ul>
</li>
</ul>
<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyVariableParameter4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// static &lt;E&gt;  List&lt;E&gt;  of(E…elements)  创建一个具有指定元素的List集合对象</span></span><br><span class="line">        <span class="comment">//static &lt;E&gt;  Set&lt;E&gt;  of(E…elements)    创建一个具有指定元素的Set集合对象</span></span><br><span class="line">        <span class="comment">//static &lt;K , V&gt;   Map&lt;K，V&gt;  of(E…elements) 创建一个具有指定元素的Map集合对象</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//method1();</span></span><br><span class="line">        <span class="comment">//method2();</span></span><br><span class="line">        <span class="comment">//method3();</span></span><br><span class="line">        <span class="comment">//method4();</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method4</span><span class="params">()</span> &#123;</span><br><span class="line">        Map&lt;String, String&gt; map = Map.ofEntries(</span><br><span class="line">                Map.entry(<span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;江苏&quot;</span>),</span><br><span class="line">                Map.entry(<span class="string">&quot;lisi&quot;</span>, <span class="string">&quot;北京&quot;</span>));</span><br><span class="line">        System.out.println(map);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method3</span><span class="params">()</span> &#123;</span><br><span class="line">        Map&lt;String, String&gt; map = Map.of(<span class="string">&quot;zhangsan&quot;</span>, <span class="string">&quot;江苏&quot;</span>, <span class="string">&quot;lisi&quot;</span>, <span class="string">&quot;北京&quot;</span>, <span class="string">&quot;wangwu&quot;</span>, <span class="string">&quot;天津&quot;</span>);</span><br><span class="line">        System.out.println(map);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//传递的参数当中，不能存在重复的元素。</span></span><br><span class="line">        Set&lt;String&gt; set = Set.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>,<span class="string">&quot;a&quot;</span>);</span><br><span class="line">        System.out.println(set);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;String&gt; list = List.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>);</span><br><span class="line">        System.out.println(list);</span><br><span class="line">        <span class="comment">//list.add(&quot;Q&quot;);</span></span><br><span class="line">        <span class="comment">//list.remove(&quot;a&quot;);</span></span><br><span class="line">        <span class="comment">//list.set(0,&quot;A&quot;);</span></span><br><span class="line">        <span class="comment">//System.out.println(list);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        ArrayList&lt;String&gt; list2 = new ArrayList&lt;&gt;();</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"><span class="comment">//        list2.add(&quot;aaa&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//集合的批量添加。</span></span><br><span class="line">        <span class="comment">//首先是通过调用List.of方法来创建一个不可变的集合，of方法的形参就是一个可变参数。</span></span><br><span class="line">        <span class="comment">//再创建一个ArrayList集合，并把这个不可变的集合中所有的数据，都添加到ArrayList中。</span></span><br><span class="line">        ArrayList&lt;String&gt; list3 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(List.of(<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>, <span class="string">&quot;d&quot;</span>));</span><br><span class="line">        System.out.println(list3);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/02/13/3.%E5%B8%B8%E7%94%A8API/">3.常用API</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-13</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-09-14</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-API"   >
          <a href="#1-API" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-API" class="headerlink" title="1.API"></a>1.API</h1>
      
        <h2 id="1-1-API概述"   >
          <a href="#1-1-API概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1-API概述" class="headerlink" title="1.1 API概述"></a>1.1 API概述</h2>
      <ul>
<li><p>什么是API</p>
<p>​	API (Application Programming Interface) ：应用程序编程接口</p>
</li>
<li><p>java中的API</p>
<p>​	指的就是 JDK 中提供的各种功能的 Java类，这些类将底层的实现封装了起来，我们不需要关心这些类是如何实现的，只需要学习这些类如何使用即可，我们可以通过帮助文档来学习这些API如何使用。</p>
</li>
</ul>

        <h2 id="1-2-键盘录入字符串"   >
          <a href="#1-2-键盘录入字符串" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-键盘录入字符串" class="headerlink" title="1.2 键盘录入字符串"></a>1.2 键盘录入字符串</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Scanner类 :</span><br><span class="line"></span><br><span class="line">		next() <span class="comment">//遇到了空格, 就不再录入数据了 , 结束标记: 空格, tab键</span></span><br><span class="line"></span><br><span class="line">		nextLine() <span class="comment">//可以将数据完整的接收过来 , 结束标记: 回车换行符       </span></span><br></pre></td></tr></table></div></figure>

<p><strong>代码实现 :</strong> </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.api;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Scanner</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        nextInt和nextLine方法配合使用的时候, nextLine方法就没有键盘录入的机会了</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        建议: 今后键盘录入数据的时候, 如果是字符串和整数一起接受, 建议使用next方法接受字符串.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入整数:&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> sc.nextInt(); <span class="comment">// 10 + 回车换行后，程序直接就结束了</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入字符串:&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> sc.nextLine();</span><br><span class="line">      	<span class="comment">//String s = sc.next();//改成这个就没有问题了</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        System.out.println(num);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><img src="https://s2.loli.net/2022/02/13/IVlkUfGixAwSJ5D.png" alt="image-20220213181941454"></p>

        <h1 id="2-String类"   >
          <a href="#2-String类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-String类" class="headerlink" title="2.String类"></a>2.String类</h1>
      
        <h2 id="2-1-String概述"   >
          <a href="#2-1-String概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-String概述" class="headerlink" title="2.1 String概述"></a>2.1 String概述</h2>
      <p>​	1 String 类在 java.lang 包下，所以使用的时候不需要导包</p>
<p>​	2 String 类代表字符串，Java 程序中的所有字符串文字（例如“abc”）都被实现为此类的实例也就是说，Java 程序中所有的双引号字符串，都是 String 类的对象</p>
<p>​	3 字符串不可变，它们的值在创建后不能被更改</p>

        <h2 id="2-2-String类的构造方法"   >
          <a href="#2-2-String类的构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-String类的构造方法" class="headerlink" title="2.2 String类的构造方法"></a>2.2 String类的构造方法</h2>
      <p>常用的构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">()</span>	<span class="comment">//创建个空白字符串对象,不含有任何内容</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">(<span class="type">char</span>[] chs)</span>	<span class="comment">//根据字符数组的内容,来创建字符串对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">String</span><span class="params">(String original)</span>	<span class="comment">//根据传入的字符串内容,来创建字符串对象</span></span><br><span class="line">  </span><br><span class="line"><span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>	<span class="comment">//直接赋值的方式创建字符串对象,内容就是abc</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.string;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2StringConstructor</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        String类常见构造方法:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            public String() : 创建一个空白字符串对象，不含有任何内容</span></span><br><span class="line"><span class="comment">            public String(char[] chs) : 根据字符数组的内容，来创建字符串对象</span></span><br><span class="line"><span class="comment">            public String(String original) : 根据传入的字符串内容，来创建字符串对象</span></span><br><span class="line"><span class="comment">            String s = “abc”;  直接赋值的方式创建字符串对象，内容就是abc</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">         注意:</span></span><br><span class="line"><span class="comment">                String这个类比较特殊, 打印其对象名的时候, 不会出现内存地址</span></span><br><span class="line"><span class="comment">                而是该对象所记录的真实内容.</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                面向对象-继承, Object类</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// public String() : 创建一个空白字符串对象，不含有任何内容</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>();</span><br><span class="line">        System.out.println(s1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// public String(char[] chs) : 根据字符数组的内容，来创建字符串对象</span></span><br><span class="line">        <span class="type">char</span>[] chs = &#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>&#125;;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(chs);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// public String(String original) : 根据传入的字符串内容，来创建字符串对象</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        System.out.println(s3);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="2-3-创建字符串对象的区别对比"   >
          <a href="#2-3-创建字符串对象的区别对比" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-创建字符串对象的区别对比" class="headerlink" title="2.3 创建字符串对象的区别对比"></a>2.3 创建字符串对象的区别对比</h2>
      <ul>
<li><p><strong>通过构造方法创建</strong></p>
<p>​	通过 new 创建的字符串对象，每一次 new 都会申请一个内存空间，虽然内容相同，但是地址值不同</p>
</li>
<li><p><strong>直接赋值方式创建</strong></p>
<p>​	以“”方式给出的字符串，只要字符序列相同(顺序和大小写)，无论在程序代码中出现几次，JVM 都只会建立一个 String 对象，并在字符串池中维护</p>
</li>
</ul>

        <h2 id="2-4-字符串的比较"   >
          <a href="#2-4-字符串的比较" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-字符串的比较" class="headerlink" title="2.4 字符串的比较"></a>2.4 字符串的比较</h2>
      
        <h3 id="2-4-1-字符串的比较"   >
          <a href="#2-4-1-字符串的比较" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-4-1-字符串的比较" class="headerlink" title="2.4.1 字符串的比较"></a>2.4.1 字符串的比较</h3>
      <ul>
<li>&#x3D;&#x3D; 比较基本数据类型：比较的是具体的值</li>
<li>&#x3D;&#x3D; 比较引用数据类型：比较的是对象地址值</li>
</ul>
<p>String类 :  <code>public boolean equals(String s)</code> <strong>比较两个字符串内容是否相同、区分大小写</strong></p>
<p><strong>代码 :</strong></p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.stringmethod;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Equals</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="string">&quot;ABC&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// equals : 比较字符串内容, 区分大小写</span></span><br><span class="line">        System.out.println(s1.equals(s2));<span class="comment">//false</span></span><br><span class="line">        System.out.println(s1.equals(s3));<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// equalsIgnoreCase : 比较字符串内容, 忽略大小写</span></span><br><span class="line">        System.out.println(s1.equalsIgnoreCase(s2));<span class="comment">//true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="2-5-String常用方法"   >
          <a href="#2-5-String常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-5-String常用方法" class="headerlink" title="2.5 String常用方法"></a>2.5 String常用方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object anObject)</span>  <span class="comment">//比较字符串的内容，严格区分大小写</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equalsIgnoreCase</span><span class="params">(String anotherString)</span>  <span class="comment">//比较字符串的内容，忽略大小写</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">length</span><span class="params">()</span>  <span class="comment">//返回此字符串的长度</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">char</span> <span class="title function_">charAt</span><span class="params">(<span class="type">int</span> index)</span>  <span class="comment">//返回指定索引处的 char 值</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">char</span>[] toCharArray()  <span class="comment">//将字符串拆分为字符数组后返回</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">substring</span><span class="params">(<span class="type">int</span> beginIndex, <span class="type">int</span> endIndex)</span>  <span class="comment">//根据开始和结束索引进行截取，得到新的字符串（包含头，不包含尾）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">substring</span><span class="params">(<span class="type">int</span> beginIndex)</span>  <span class="comment">//从传入的索引处截取，截取到末尾，得到新的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">replace</span><span class="params">(CharSequence target, CharSequence replacement)</span>  <span class="comment">//使用新值，将字符串中的旧值替换，得到新的字符串</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String[] split(String regex)  <span class="comment">//根据传入的规则切割字符串，得到字符串数组</span></span><br></pre></td></tr></table></div></figure>


        <h1 id="3-StringBuilder类"   >
          <a href="#3-StringBuilder类" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-StringBuilder类" class="headerlink" title="3.StringBuilder类"></a>3.StringBuilder类</h1>
      
        <h2 id="3-1-StringBuilder类概述"   >
          <a href="#3-1-StringBuilder类概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-StringBuilder类概述" class="headerlink" title="3.1 StringBuilder类概述"></a>3.1 StringBuilder类概述</h2>
      <p>​	StringBuilder 是一个可变的字符串类，我们可以把它看成是一个容器，这里的可变指的是 StringBuilder 对象中的内容是可变的</p>

        <h2 id="3-2-StringBuilder类和String类的区别"   >
          <a href="#3-2-StringBuilder类和String类的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-StringBuilder类和String类的区别" class="headerlink" title="3.2 StringBuilder类和String类的区别"></a>3.2 StringBuilder类和String类的区别</h2>
      <ul>
<li>String类：内容是<strong>不可变</strong>的</li>
<li>StringBuilder类：内容是<strong>可变</strong>的</li>
</ul>

        <h2 id="3-3-StringBuilder类的构造方法"   >
          <a href="#3-3-StringBuilder类的构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-3-StringBuilder类的构造方法" class="headerlink" title="3.3 StringBuilder类的构造方法"></a>3.3 StringBuilder类的构造方法</h2>
      <p>常用的构造方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">()</span>	<span class="comment">//创建一个空白可变字符串对象，不含有任何内容</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">(String str)</span>	<span class="comment">//根据字符串的内容，来创建可变字符串对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public StringBuilder()：创建一个空白可变字符串对象，不含有任何内容</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);		<span class="comment">//sb:</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb.length():&quot;</span> + sb.length());		<span class="comment">//sb.length():0</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder(String str)：根据字符串的内容，来创建可变字符串对象</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;sb2:&quot;</span> + sb2);		<span class="comment">//sb2:hello</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb2.length():&quot;</span> + sb2.length());		<span class="comment">//sb2.length():5</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-4-StringBuilder常用的成员方法"   >
          <a href="#3-4-StringBuilder常用的成员方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-4-StringBuilder常用的成员方法" class="headerlink" title="3.4 StringBuilder常用的成员方法"></a>3.4 StringBuilder常用的成员方法</h2>
      <ul>
<li>添加和反转方法</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> StringBuilder <span class="title function_">append</span><span class="params">(任意类型)</span>	<span class="comment">//添加数据，并返回对象本身</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> StringBuilder <span class="title function_">reverse</span><span class="params">()</span>	<span class="comment">//返回相反的字符序列</span></span><br></pre></td></tr></table></div></figure>

<ul>
<li>示例代码</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建对象</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder append(任意类型)：添加数据，并返回对象本身</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> sb.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb); <span class="comment">//sb:hello</span></span><br><span class="line">        System.out.println(<span class="string">&quot;sb2:&quot;</span> + sb2);<span class="comment">//sb2:hello</span></span><br><span class="line">        System.out.println(sb == sb2);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">        sb.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        sb.append(<span class="string">&quot;world&quot;</span>);</span><br><span class="line">        sb.append(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        sb.append(<span class="number">100</span>);</span><br><span class="line">        <span class="comment">//链式编程</span></span><br><span class="line">        sb.append(<span class="string">&quot;hello&quot;</span>).append(<span class="string">&quot;world&quot;</span>).append(<span class="string">&quot;java&quot;</span>).append(<span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);<span class="comment">//sb:hellohelloworldjava100helloworldjava100</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public StringBuilder reverse()：返回相反的字符序列</span></span><br><span class="line">        sb.reverse();</span><br><span class="line">        System.out.println(<span class="string">&quot;sb:&quot;</span> + sb);<span class="comment">//sb:001avajdlrowolleh001avajdlrowolleholleh</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="3-5StringBuilder和String相互转换"   >
          <a href="#3-5StringBuilder和String相互转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-5StringBuilder和String相互转换" class="headerlink" title="3.5StringBuilder和String相互转换"></a>3.5StringBuilder和String相互转换</h2>
      <p>StringBuilder————》String:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span><span class="comment">//通过 toString() 就可以实现把 StringBuilder 转换为 String</span></span><br></pre></td></tr></table></div></figure>

<p>String————》StringBuilder:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">StringBuilder</span><span class="params">(String s)</span><span class="comment">//通过构造方法就可以实现把 String 转换为 StringBuilder</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringBuilderDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//StringBuilder 转换为 String</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">        sb1.append(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">        <span class="comment">//String s1 = sb; //这个是错误的做法</span></span><br><span class="line">        <span class="comment">//public String toString()：通过 toString() 就可以实现把 StringBuilder 转换为 String</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> sb1.toString();</span><br><span class="line">        System.out.println(s1);	<span class="comment">//hello</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//String 转换为 StringBuilder</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        <span class="comment">//StringBuilder sb = s; //这个是错误的做法</span></span><br><span class="line">        <span class="comment">//public StringBuilder(String s)：通过构造方法就可以实现把 String 转换为 StringBuilder</span></span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(s2);</span><br><span class="line">        System.out.println(sb2);	<span class="comment">//hello</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="4-Math类"   >
          <a href="#4-Math类" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-Math类" class="headerlink" title="4.Math类"></a>4.Math类</h1>
      <p>1、Math类概述：包含执行基本数字运算的方法</p>
<p>2、Math中方法的调用方式：Math类中无构造方法，但内部的方法都是静态的，可以通过   <code>类名.方法名</code>进行调用</p>
<p>3、Math类的常用方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">abs</span><span class="params">(<span class="type">int</span> a)</span>	<span class="comment">//返回参数的绝对值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">ceil</span> <span class="params">(<span class="type">double</span> a)</span>	<span class="comment">//返回大于或等于参数的最小 double值,等于ー个整数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">floor</span><span class="params">(<span class="type">double</span> a)</span>	<span class="comment">//返回小于或等于参数的最大 double值,等于一个整数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">round</span><span class="params">(<span class="type">float</span> a)</span>	<span class="comment">//按照四舍五入返回最接近参数的int</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>	<span class="comment">//返回两个int值中的较大值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">min</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>	<span class="comment">//返回两个int值中的较小值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">pow</span><span class="params">( <span class="type">double</span> a, <span class="type">double</span> b)</span>	<span class="comment">//返回a的b次幂的值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">double</span> <span class="title function_">random</span><span class="params">()</span>	<span class="comment">//返回值为 double的正值,[0.0,1.0)</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="5-System类"   >
          <a href="#5-System类" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-System类" class="headerlink" title="5.System类"></a>5.System类</h1>
      <p>System类的常用方法 </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">exit</span><span class="params">(<span class="type">int</span> status)</span>		<span class="comment">//终止当前运行的Java虚拟机，非零表示异常终止</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">long</span> <span class="title function_">currentTimeMillis</span><span class="params">()</span>	<span class="comment">//返回当前时间(以毫秒为单位)</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">arraycopy</span><span class="params">(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)</span>	<span class="comment">//数组copy</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="6-Object类"   >
          <a href="#6-Object类" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-Object类" class="headerlink" title="6.Object类"></a>6.Object类</h1>
      <p>Object 是所有类的根，每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类，换句话说，该类所具备的方法，所有类都会有一份。</p>

        <h2 id="6-1-toString方法"   >
          <a href="#6-1-toString方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-toString方法" class="headerlink" title="6.1 toString方法"></a>6.1 toString方法</h2>
      <p>为了能以良好的格式，更方便的展示对象中的属性值，通常会在类中重写方法，把展示地址变为展示属性。</p>
<p>原始方法：</p>
<p><img src="https://s2.loli.net/2022/02/13/zPuypmwNJFTSLds.png" alt="toString方法的源码分析"></p>
<p>重写后：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">extends</span> <span class="title class_">Object</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s.setAge(<span class="number">30</span>);</span><br><span class="line">        System.out.println(s); </span><br><span class="line">        System.out.println(s.toString()); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>运行结果：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Student&#123;name=<span class="string">&#x27;林青霞&#x27;</span>, age=<span class="number">30</span>&#125;</span><br><span class="line">Student&#123;name=<span class="string">&#x27;林青霞&#x27;</span>, age=<span class="number">30</span>&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-2-equals方法"   >
          <a href="#6-2-equals方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-equals方法" class="headerlink" title="6.2 equals方法"></a>6.2 equals方法</h2>
      <p>重写equals方法的场景：不希望比较对象的地址值，想要结合对象属性进行比较的时候</p>
<p>原始方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object obj)</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> (<span class="built_in">this</span> == obj);<span class="comment">//比的是地址值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>示例代码：</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">        <span class="comment">//this -- s1</span></span><br><span class="line">        <span class="comment">//o -- s2</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student) o; <span class="comment">//student -- s2</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (age != student.age) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">return</span> name != <span class="literal">null</span> ? name.equals(student.name) : student.name == <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ObjectDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s1.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s1.setAge(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        s2.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s2.setAge(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//需求：比较两个对象的内容是否相同</span></span><br><span class="line">        System.out.println(s1.equals(s2));</span><br><span class="line">        <span class="comment">//重写了equals，就是true</span></span><br><span class="line">      	<span class="comment">//没重写，比的就是地址，返回false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>面试题</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InterviewTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>(<span class="string">&quot;abc&quot;</span>);</span><br><span class="line">        <span class="comment">//此时调用的是String类中的equals方法。</span></span><br><span class="line">      	<span class="comment">//虽然String重写了equals方法，但方法中写了，若参数不是字符串，则直接fals；若参数是字符串才进行内容比较</span></span><br><span class="line">        System.out.println(s1.equals(sb)); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//StringBuilder类中是没有重写equals方法,用的就是Object类中的，比的就是地址值</span></span><br><span class="line">        System.out.println(sb.equals(s1)); <span class="comment">// false</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="6-3-其他方法"   >
          <a href="#6-3-其他方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-其他方法" class="headerlink" title="6.3 其他方法"></a>6.3 其他方法</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(对象)</span>	<span class="comment">//返回参数中对象的字符串表示形式。</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(对象, 默认字符串)</span>	<span class="comment">//返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Boolean <span class="title function_">isNull</span><span class="params">(对象)</span>	<span class="comment">//判断对象是否为空</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Boolean <span class="title function_">nonNull</span><span class="params">(对象)</span>	<span class="comment">//判断对象是否不为空  </span></span><br></pre></td></tr></table></div></figure>




        <h1 id="7-BigDecimal类"   >
          <a href="#7-BigDecimal类" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-BigDecimal类" class="headerlink" title="7.BigDecimal类"></a>7.BigDecimal类</h1>
      <p>作用：可以用来进行精确计算</p>
<p>构造方法</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">BigDecimal(<span class="type">double</span> val)	<span class="comment">//参数为double</span></span><br><span class="line">  </span><br><span class="line">BigDecimal(String val)	<span class="comment">//参数为String</span></span><br></pre></td></tr></table></div></figure>

<p>常用方法</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">add</span><span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//加法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">subtract</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//减法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">multiply</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//乘法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">divide</span> <span class="params">(另一个BigDecimal对象)</span>	<span class="comment">//除法</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> BigDecimal <span class="title function_">divide</span> <span class="params">(另一个BigDecimal对象，精确几位，舍入模式)</span>	<span class="comment">//除法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_UP  进一法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_FLOOR 去尾法</span></span><br><span class="line"><span class="comment">//  BigDecimal.ROUND_HALF_UP 四舍五入</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="8-包装类"   >
          <a href="#8-包装类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-包装类" class="headerlink" title="8.包装类"></a>8.包装类</h1>
      
        <h2 id="8-1-基本类型包装类"   >
          <a href="#8-1-基本类型包装类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-基本类型包装类" class="headerlink" title="8.1 基本类型包装类"></a>8.1 基本类型包装类</h2>
      <p>基本类型包装类的作用：将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据</p>
<p>常用的操作之一：用于基本数据类型与字符串之间的转换</p>
<p>基本类型对应的包装类：</p>
<div class="table-container"><table>
<thead>
<tr>
<th>基本数据类型</th>
<th>包装类</th>
</tr>
</thead>
<tbody><tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
<tr>
<td>char</td>
<td>Character</td>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
</tr>
</tbody></table></div>

        <h2 id="8-2-自动拆箱和自动装箱"   >
          <a href="#8-2-自动拆箱和自动装箱" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-自动拆箱和自动装箱" class="headerlink" title="8.2 自动拆箱和自动装箱"></a>8.2 自动拆箱和自动装箱</h2>
      <p>自动装箱：把基本数据类型转换为对应的包装类类型</p>
<p>自动拆箱：把包装类类型转换为对应的基本数据类型</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Integer</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">100</span>;  <span class="comment">// 自动装箱</span></span><br><span class="line">i += <span class="number">200</span>;         <span class="comment">// i = i + 200;  i + 200 自动拆箱；i = i + 200; 是自动装箱</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="8-3-Integer类"   >
          <a href="#8-3-Integer类" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-Integer类" class="headerlink" title="8.3 Integer类"></a>8.3 Integer类</h2>
      <p>Integer类概述：包装一个对象中的原始类型 int 的值</p>
<p>Integer类构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">Integer</span><span class="params">(<span class="type">int</span> value)</span>	<span class="comment">//根据int值创建Integer对象(过时)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">Integer</span><span class="params">(String s)</span>	<span class="comment">//根据String值创建Integer对象(过时)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Integer <span class="title function_">valueOf</span><span class="params">(<span class="type">int</span> i)</span>	<span class="comment">//返回表示指定的int值的Integer实例</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Integer <span class="title function_">valueOf</span><span class="params">(String s)</span>	<span class="comment">//把String转为Integer类型</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public Integer(int value)：根据 int 值创建 Integer 对象(过时)</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Integer</span>(<span class="number">100</span>);</span><br><span class="line">        System.out.println(i1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public Integer(String s)：根据 String 值创建 Integer 对象(过时)</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Integer</span>(<span class="string">&quot;100&quot;</span>);</span><br><span class="line"><span class="comment">//        Integer i2 = new Integer(&quot;abc&quot;); //NumberFormatException</span></span><br><span class="line">        System.out.println(i2);</span><br><span class="line">        System.out.println(<span class="string">&quot;--------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public static Integer valueOf(int i)：返回表示指定的 int 值的 Integer 实例</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i3</span> <span class="operator">=</span> Integer.valueOf(<span class="number">100</span>);</span><br><span class="line">        System.out.println(i3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public static Integer valueOf(String s)：返回一个保存指定值的Integer对象 String</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i4</span> <span class="operator">=</span> Integer.valueOf(<span class="string">&quot;100&quot;</span>);</span><br><span class="line">        System.out.println(i4);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-4int和String类型的相互转换"   >
          <a href="#8-4int和String类型的相互转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4int和String类型的相互转换" class="headerlink" title="8.4int和String类型的相互转换"></a>8.4int和String类型的相互转换</h2>
      
        <h3 id="8-4-1-int转换为String"   >
          <a href="#8-4-1-int转换为String" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-1-int转换为String" class="headerlink" title="8.4.1 int转换为String"></a>8.4.1 int转换为String</h3>
      <p>转换方式:</p>
<ul>
<li>方式一：直接在数字后加一个空字符串</li>
<li>方式二：通过String类静态方法valueOf()</li>
</ul>
<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//int ---&gt; String</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式1</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> number + <span class="string">&quot;&quot;</span>;</span><br><span class="line">        System.out.println(s1);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//public static String valueOf(int i)</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> String.valueOf(number);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="8-4-2-String转换为int"   >
          <a href="#8-4-2-String转换为int" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-2-String转换为int" class="headerlink" title="8.4.2 String转换为int"></a>8.4.2 String转换为int</h3>
      <p>转换方式：</p>
<ul>
<li>方式一：先将字符串数字转成Integer，再调用valueOf()方法</li>
<li>方式二：通过Integer静态方法parseInt()进行转换</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//String ---&gt; int</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;100&quot;</span>;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式1：String --- Integer --- int</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i</span> <span class="operator">=</span> Integer.valueOf(s);</span><br><span class="line">        <span class="comment">//public int intValue()</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> i.intValue();</span><br><span class="line">        System.out.println(x);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//public static int parseInt(String s)</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> Integer.parseInt(s);</span><br><span class="line">        System.out.println(y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="9-Arrays类"   >
          <a href="#9-Arrays类" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-Arrays类" class="headerlink" title="9.Arrays类"></a>9.Arrays类</h1>
      <p>常用方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title function_">toString</span><span class="params">(<span class="type">int</span>[] a)</span>	<span class="comment">//返回指定数组的内容的字符串表示形式</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sort</span><span class="params">(<span class="type">int</span>[] a)</span>	<span class="comment">//按照数字顺序排列指定的数组</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">binarySearch</span><span class="params">(<span class="type">int</span>[] a, <span class="type">int</span> key)</span>	<span class="comment">//利用二分查找返回指定元素的索引</span></span><br></pre></td></tr></table></div></figure>






        <h1 id="10-时间日期类"   >
          <a href="#10-时间日期类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-时间日期类" class="headerlink" title="10.时间日期类"></a>10.时间日期类</h1>
      
        <h2 id="10-1-Date类"   >
          <a href="#10-1-Date类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-Date类" class="headerlink" title="10.1 Date类"></a>10.1 Date类</h2>
      <p>计算机中时间原点：1970年1月1日 00:00:00</p>
<p>时间换算单位：1秒 &#x3D; 1000毫秒</p>
<p>Date类概述：Date 代表了一个特定的时间，精确到毫秒</p>

        <h3 id="10-1-1-Date类构造方法"   >
          <a href="#10-1-1-Date类构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-1-Date类构造方法" class="headerlink" title="10.1.1 Date类构造方法"></a>10.1.1 Date类构造方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">Date</span><span class="params">()</span>	<span class="comment">//分配一个 Date对象，并初始化，以便它代表它被分配的时间，精确到毫秒</span></span><br><span class="line"><span class="keyword">public</span> <span class="title function_">Date</span><span class="params">(<span class="type">long</span> date)</span>	<span class="comment">//分配一个 Date对象，并将其初始化为表示从标准基准时间起指定的毫秒数</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DateDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public Date()：分配一个 Date对象，并初始化，以便它代表它被分配的时间，精确到毫秒</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        System.out.println(d1);</span><br><span class="line">      	<span class="comment">//Sun Feb 13 21:16:31 CST 2022	此时的时间</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public Date(long date)：分配一个 Date对象，并将其初始化为表示从标准基准时间起指定的毫秒数</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">date</span> <span class="operator">=</span> <span class="number">1000</span>*<span class="number">60</span>*<span class="number">60</span>;</span><br><span class="line">        <span class="type">Date</span> <span class="variable">d2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>(date);</span><br><span class="line">        System.out.println(d2);</span><br><span class="line">      	<span class="comment">//Thu Jan 01 09:00:00 CST 1970	从时间原点往后算data微秒的时刻</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-1-2-常用方法"   >
          <a href="#10-1-2-常用方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-2-常用方法" class="headerlink" title="10.1.2 常用方法"></a>10.1.2 常用方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">getTime</span><span class="params">()</span>	<span class="comment">//获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setTime</span><span class="params">(<span class="type">long</span> time)</span>	<span class="comment">//设置时间，给的是毫秒值</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DateDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建日期对象</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值</span></span><br><span class="line">        System.out.println(d.getTime());    <span class="comment">//1644758475285</span></span><br><span class="line">        System.out.println(d.getTime() * <span class="number">1.0</span> / <span class="number">1000</span> / <span class="number">60</span> / <span class="number">60</span> / <span class="number">24</span> / <span class="number">365</span> + <span class="string">&quot;年&quot;</span>);</span><br><span class="line">        <span class="comment">//52.15494911482116年</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public void setTime(long time):设置时间，给的是毫秒值</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">time</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        d.setTime(time);    <span class="comment">//Sun Feb 13 21:21:15 CST 2022</span></span><br><span class="line"></span><br><span class="line">        System.out.println(d);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h2 id="10-2-SimpleDateFormat类"   >
          <a href="#10-2-SimpleDateFormat类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-2-SimpleDateFormat类" class="headerlink" title="10.2 SimpleDateFormat类"></a>10.2 SimpleDateFormat类</h2>
      <p>概述：SimpleDateFormat是一个具体的类，用于以区域设置敏感的方式格式化和解析日期。</p>
<p>构造方法：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">SimpleDateFormat</span><span class="params">()</span>	<span class="comment">//构造一个SimpleDateFormat，使用默认模式和日期格式</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="title function_">SimpleDateFormat</span><span class="params">(String pattern)</span>	<span class="comment">//构造一个SimpleDateFormat使用给定的模式和默认的日期格式</span></span><br></pre></td></tr></table></div></figure>

<p>常用方法:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//格式化(从Date到String)</span></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String <span class="title function_">format</span><span class="params">(Date date)</span>	<span class="comment">//将日期格式化成日期/时间字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="comment">//解析(从String到Date)</span></span><br><span class="line">  <span class="keyword">public</span> Date <span class="title function_">parse</span><span class="params">(String source)</span>	<span class="comment">//从给定字符串的开始解析文本以生成日期</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SimpleDateFormatDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ParseException &#123;</span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//格式化：从 Date 到 String</span></span><br><span class="line"><span class="comment">//        SimpleDateFormat sdf = new SimpleDateFormat();//2022/2/13 下午9:28</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="comment">//2022年02月13日 21:28:53</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> sdf.format(d);</span><br><span class="line">        System.out.println(s);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//解析：从 String 到 Date</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">ss</span> <span class="operator">=</span> <span class="string">&quot;2048-08-09 11:11:11&quot;</span>;</span><br><span class="line">        <span class="comment">//ParseException</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">Date</span> <span class="variable">dd</span> <span class="operator">=</span> sdf2.parse(ss);</span><br><span class="line">        System.out.println(dd);<span class="comment">//Sun Aug 09 11:11:11 CST 2048</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="10-3-JDK8时间日期类"   >
          <a href="#10-3-JDK8时间日期类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-JDK8时间日期类" class="headerlink" title="10.3 JDK8时间日期类"></a>10.3 JDK8时间日期类</h2>
      <ul>
<li>LocalDate       表示日期（年月日）  </li>
<li>LocalTime       表示时间（时分秒）</li>
<li>LocalDateTime    表示时间+ 日期 （年月日时分秒）</li>
</ul>

        <h3 id="10-3-1-LocalDateTime创建方法"   >
          <a href="#10-3-1-LocalDateTime创建方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-1-LocalDateTime创建方法" class="headerlink" title="10.3.1 LocalDateTime创建方法"></a>10.3.1 LocalDateTime创建方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title function_">now</span><span class="params">()</span>	<span class="comment">//获取当前系统时间</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> LocalDateTime <span class="title function_">of</span><span class="params">(年,月,日,时,分,秒)</span>	<span class="comment">//使用指定年月日和时分秒初始化一个LocalDateTime对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">now</span> <span class="operator">=</span> LocalDateTime.now();</span><br><span class="line">        System.out.println(now);<span class="comment">//2022-02-13T22:36:17.005158</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>);</span><br><span class="line">        System.out.println(localDateTime);<span class="comment">//2020-11-11T11:11:11</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-2-LocalDateTime获取方法"   >
          <a href="#10-3-2-LocalDateTime获取方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-2-LocalDateTime获取方法" class="headerlink" title="10.3.2 LocalDateTime获取方法"></a>10.3.2 LocalDateTime获取方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getYear</span><span class="params">()</span>	<span class="comment">//获取年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMonthValue</span><span class="params">()</span>	<span class="comment">//获取月份(1-12)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDayofMonth</span><span class="params">()</span>	<span class="comment">//获取月份中的第几天(1-31)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDayofYeara</span><span class="params">()</span>	<span class="comment">//获取一年中的第几天(1-366)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> DayofWeek <span class="title function_">getDayofWeek</span><span class="params">()</span>	<span class="comment">//获取星期</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMinute</span><span class="params">()</span>	<span class="comment">//获取分钟</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getHour</span><span class="params">()</span>	<span class="comment">//获取小时</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="comment">//public int getYear()           获取年</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">year</span> <span class="operator">=</span> localDateTime.getYear();</span><br><span class="line">        System.out.println(<span class="string">&quot;年为&quot;</span> +year);<span class="comment">//年为2020</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getMonthValue()     获取月份（1-12）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">month</span> <span class="operator">=</span> localDateTime.getMonthValue();</span><br><span class="line">        System.out.println(<span class="string">&quot;月份为&quot;</span> + month);<span class="comment">//月份为11</span></span><br><span class="line"></span><br><span class="line">        <span class="type">Month</span> <span class="variable">month1</span> <span class="operator">=</span> localDateTime.getMonth();</span><br><span class="line">        System.out.println(month1);<span class="comment">//NOVEMBER</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getDayOfMonth()     获取月份中的第几天（1-31）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">day</span> <span class="operator">=</span> localDateTime.getDayOfMonth();</span><br><span class="line">        System.out.println(<span class="string">&quot;日期为&quot;</span> + day);<span class="comment">//日期为11</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getDayOfYear()      获取一年中的第几天（1-366）</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">dayOfYear</span> <span class="operator">=</span> localDateTime.getDayOfYear();</span><br><span class="line">        System.out.println(<span class="string">&quot;这是一年中的第&quot;</span> + dayOfYear + <span class="string">&quot;天&quot;</span>);<span class="comment">//这是一年中的第316天</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public DayOfWeek getDayOfWeek()获取星期</span></span><br><span class="line">        <span class="type">DayOfWeek</span> <span class="variable">dayOfWeek</span> <span class="operator">=</span> localDateTime.getDayOfWeek();</span><br><span class="line">        System.out.println(<span class="string">&quot;星期为&quot;</span> + dayOfWeek);<span class="comment">//星期为WEDNESDAY</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getMinute()        获取分钟</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">minute</span> <span class="operator">=</span> localDateTime.getMinute();</span><br><span class="line">        System.out.println(<span class="string">&quot;分钟为&quot;</span> + minute);<span class="comment">//分钟为11</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//public int getHour()           获取小时</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">hour</span> <span class="operator">=</span> localDateTime.getHour();</span><br><span class="line">        System.out.println(<span class="string">&quot;小时为&quot;</span> + hour);<span class="comment">//小时为11</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-3-LocalDateTime转换方法"   >
          <a href="#10-3-3-LocalDateTime转换方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-3-LocalDateTime转换方法" class="headerlink" title="10.3.3 LocalDateTime转换方法"></a>10.3.3 LocalDateTime转换方法</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDate  <span class="title function_">toLocalDate</span> <span class="params">()</span>	<span class="comment">//转换成为一个LocalDate对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalTime <span class="title function_">toLocalTime</span> <span class="params">()</span>		<span class="comment">//转换成为一个LocalTime对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">12</span>, <span class="number">12</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">12</span>);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//public LocalDate toLocalDate ()    转换成为一个LocalDate对象</span></span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate</span> <span class="operator">=</span> localDateTime.toLocalDate();</span><br><span class="line">        System.out.println(localDate);<span class="comment">//2020-12-12</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public LocalTime toLocalTime ()    转换成为一个LocalTime对象</span></span><br><span class="line">        <span class="type">LocalTime</span> <span class="variable">localTime</span> <span class="operator">=</span> localDateTime.toLocalTime();</span><br><span class="line">        System.out.println(localTime);<span class="comment">//08:10:12</span></span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-4-LocalDateTime格式化和解析"   >
          <a href="#10-3-4-LocalDateTime格式化和解析" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-4-LocalDateTime格式化和解析" class="headerlink" title="10.3.4 LocalDateTime格式化和解析"></a>10.3.4 LocalDateTime格式化和解析</h3>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">format</span> <span class="params">(指定格式)</span>	<span class="comment">//把一个LocalDateTime格式化成为一个字符串</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">parse</span> <span class="params">(准备解析的字符串, 解析格式)</span>	<span class="comment">//把一个日期字符串解析成为一个LocalDateTime对象</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> DateTimeFormatter <span class="title function_">ofPattern</span><span class="params">(String pattern)</span>	<span class="comment">//使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo5</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//method1();</span></span><br><span class="line">        <span class="comment">//method2();</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//public String format (指定格式)   把一个LocalDateTime格式化成为一个字符串</span></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>);</span><br><span class="line">        System.out.println(localDateTime);<span class="comment">//2020-11-12T13:14:15</span></span><br><span class="line">        <span class="type">DateTimeFormatter</span> <span class="variable">pattern</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> localDateTime.format(pattern);</span><br><span class="line">        System.out.println(s);<span class="comment">//2020年11月12日 13:14:15</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//public static LocalDateTime parse (准备解析的字符串, 解析格式) 把一个日期字符串解析成为一个LocalDateTime对象</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;2020年11月12日 13:14:15&quot;</span>;</span><br><span class="line">        <span class="type">DateTimeFormatter</span> <span class="variable">pattern</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">parse</span> <span class="operator">=</span> LocalDateTime.parse(s, pattern);</span><br><span class="line">        System.out.println(parse);<span class="comment">//2020-11-12T13:14:15</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-5-LocalDateTime增加或者减少时间的方法"   >
          <a href="#10-3-5-LocalDateTime增加或者减少时间的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-5-LocalDateTime增加或者减少时间的方法" class="headerlink" title="10.3.5 LocalDateTime增加或者减少时间的方法"></a>10.3.5 LocalDateTime增加或者减少时间的方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusYears</span> <span class="params">(<span class="type">long</span> years)</span>	<span class="comment">//添加或者减去年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusMonths</span><span class="params">(<span class="type">long</span> months)</span>	<span class="comment">//添加或者减去月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusDays</span><span class="params">(<span class="type">long</span> days)</span>	<span class="comment">//添加或者减去日</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusHours</span><span class="params">(<span class="type">long</span> hours)</span>	<span class="comment">//添加或者减去时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusMinutes</span><span class="params">(<span class="type">long</span> minutes)</span>	<span class="comment">//添加或者减去分</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusSeconds</span><span class="params">(<span class="type">long</span> seconds)</span>	<span class="comment">//添加或者减去秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">plusWeeks</span><span class="params">(<span class="type">long</span> weeks)</span>	<span class="comment">//添加或者减去周</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-6-LocalDateTime减少或者增加时间的方法"   >
          <a href="#10-3-6-LocalDateTime减少或者增加时间的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-6-LocalDateTime减少或者增加时间的方法" class="headerlink" title="10.3.6 LocalDateTime减少或者增加时间的方法"></a>10.3.6 LocalDateTime减少或者增加时间的方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusYears</span> <span class="params">(<span class="type">long</span> years)</span>	<span class="comment">//减去或者添加年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusMonths</span><span class="params">(<span class="type">long</span> months)</span>	<span class="comment">//减去或者添加月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusDays</span><span class="params">(<span class="type">long</span> days)</span>	<span class="comment">//减去或者添加日</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusHours</span><span class="params">(<span class="type">long</span> hours)</span>	<span class="comment">//减去或者添加时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusMinutes</span><span class="params">(<span class="type">long</span> minutes)</span>	<span class="comment">//减去或者添加分</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusSeconds</span><span class="params">(<span class="type">long</span> seconds)</span>	<span class="comment">//减去或者添加秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">minusWeeks</span><span class="params">(<span class="type">long</span> weeks)</span>	<span class="comment">//减去或者添加周</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="10-3-7-LocalDateTime修改方法"   >
          <a href="#10-3-7-LocalDateTime修改方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-3-7-LocalDateTime修改方法" class="headerlink" title="10.3.7 LocalDateTime修改方法"></a>10.3.7 LocalDateTime修改方法</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withYear</span><span class="params">(<span class="type">int</span> year)</span>	<span class="comment">//直接修改年</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withMonth</span><span class="params">(<span class="type">int</span> month)</span>	<span class="comment">//直接修改月</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withDayOfMonth</span><span class="params">(<span class="type">int</span> dayofmonth)</span>	<span class="comment">//直接修改日期(一个月中的第几天)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withDayOfYear</span><span class="params">(<span class="type">int</span> dayOfYear)</span>	<span class="comment">//直接修改日期(一年中的第几天)</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withHour</span><span class="params">(<span class="type">int</span> hour)</span>	<span class="comment">//直接修改小时</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withMinute</span><span class="params">(<span class="type">int</span> minute)</span>	<span class="comment">//直接修改分钟</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> LocalDateTime <span class="title function_">withSecond</span><span class="params">(<span class="type">int</span> second)</span>	<span class="comment">//直接修改秒</span></span><br></pre></td></tr></table></div></figure>




        <h2 id="10-4-Period类"   >
          <a href="#10-4-Period类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-4-Period类" class="headerlink" title="10.4 Period类"></a>10.4 Period类</h2>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> Period <span class="title function_">between</span><span class="params">(开始时间,结束时间)</span>	<span class="comment">//计算两个“时间&quot;的间隔</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getYears</span><span class="params">()</span>	<span class="comment">//获得这段时间的年数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getMonths</span><span class="params">()</span>	<span class="comment">//获得此期间的总月数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getDays</span><span class="params">()</span>	<span class="comment">//获得此期间的天数</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">toTotalMonths</span><span class="params">()</span>	<span class="comment">//获取此期间的总月数</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  计算两个时间的间隔</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo9</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public static Period between(开始时间,结束时间)  计算两个&quot;时间&quot;的间隔</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate1</span> <span class="operator">=</span> LocalDate.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">        <span class="type">LocalDate</span> <span class="variable">localDate2</span> <span class="operator">=</span> LocalDate.of(<span class="number">2048</span>, <span class="number">12</span>, <span class="number">12</span>);</span><br><span class="line">        <span class="type">Period</span> <span class="variable">period</span> <span class="operator">=</span> Period.between(localDate1, localDate2);</span><br><span class="line">        System.out.println(period);<span class="comment">//P28Y11M11D</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public int getYears()         获得这段时间的年数</span></span><br><span class="line">        System.out.println(period.getYears());<span class="comment">//28</span></span><br><span class="line">        <span class="comment">//public int getMonths()        获得此期间的月数</span></span><br><span class="line">        System.out.println(period.getMonths());<span class="comment">//11</span></span><br><span class="line">        <span class="comment">//public int getDays()          获得此期间的天数</span></span><br><span class="line">        System.out.println(period.getDays());<span class="comment">//11</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//public long toTotalMonths()   获取此期间的总月数</span></span><br><span class="line">        System.out.println(period.toTotalMonths());<span class="comment">//347</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h2 id="10-5-Duration类"   >
          <a href="#10-5-Duration类" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-5-Duration类" class="headerlink" title="10.5 Duration类"></a>10.5 Duration类</h2>
      <p>方法说明:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="title function_">Durationbetween</span><span class="params">(开始时间,结束时间)</span>	<span class="comment">//计算两个“时间&quot;的间隔</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">long</span> <span class="title function_">toSeconds</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">toMillis</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的毫秒</span></span><br><span class="line">  </span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">toNanos</span><span class="params">()</span>	<span class="comment">//获得此时间间隔的纳秒</span></span><br></pre></td></tr></table></div></figure>

<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  计算两个时间的间隔</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDK8DateDemo10</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//public static Duration between(开始时间,结束时间)  计算两个“时间&quot;的间隔</span></span><br><span class="line"></span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime1</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">13</span>, <span class="number">14</span>, <span class="number">15</span>);</span><br><span class="line">        <span class="type">LocalDateTime</span> <span class="variable">localDateTime2</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2020</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="type">Duration</span> <span class="variable">duration</span> <span class="operator">=</span> Duration.between(localDateTime1, localDateTime2);</span><br><span class="line">        System.out.println(duration);<span class="comment">//PT21H57M58S</span></span><br><span class="line">        <span class="comment">//public long toSeconds()	       获得此时间间隔的秒</span></span><br><span class="line">        System.out.println(duration.toSeconds());<span class="comment">//79078</span></span><br><span class="line">        <span class="comment">//public int toMillis()	           获得此时间间隔的毫秒</span></span><br><span class="line">        System.out.println(duration.toMillis());<span class="comment">//79078000</span></span><br><span class="line">        <span class="comment">//public int toNanos()             获得此时间间隔的纳秒</span></span><br><span class="line">        System.out.println(duration.toNanos());<span class="comment">//79078000000000</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/02/13/2.%E9%9D%A2%E5%AF%B9%E5%AF%B9%E8%B1%A1/">2.面对对象</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-13</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-02-14</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-面向对象和面向过程的思想对比"   >
          <a href="#1-面向对象和面向过程的思想对比" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-面向对象和面向过程的思想对比" class="headerlink" title="1.面向对象和面向过程的思想对比"></a>1.面向对象和面向过程的思想对比</h1>
      <p>​	<strong>面向过程</strong>：是一种以过程为中心的编程思想，实现功能的每一步，都是自己实现的</p>
<p>​	<strong>面向对象</strong>：是一种以对象为中心的编程思想，通过指挥对象实现具体的功能</p>

        <h1 id="2-类和对象的关系"   >
          <a href="#2-类和对象的关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-类和对象的关系" class="headerlink" title="2.类和对象的关系"></a>2.类和对象的关系</h1>
      <p>客观存在的事物皆为对象 ，所以我们也常常说万物皆对象。</p>

        <h2 id="2-1-类"   >
          <a href="#2-1-类" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-类" class="headerlink" title="2.1 类"></a>2.1 类</h2>
      <ul>
<li>类的理解<ul>
<li>类是对现实生活中一类具有共同属性和行为的事物的抽象</li>
<li>类是对象的数据类型，类是具有相同属性和行为的一组对象的集合</li>
<li>简单理解：类就是对现实事物的一种描述</li>
</ul>
</li>
<li>类的组成<ul>
<li>属性：指事物的特征，例如：手机事物（品牌，价格，尺寸）</li>
<li>行为：指事物能执行的操作，例如：手机事物（打电话，发短信）</li>
</ul>
</li>
</ul>

        <h2 id="2-2-类和对象的关系"   >
          <a href="#2-2-类和对象的关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-类和对象的关系" class="headerlink" title="2.2 类和对象的关系"></a>2.2 类和对象的关系</h2>
      <ul>
<li>类：类是对现实生活中一类具有共同属性和行为的事物的抽象</li>
<li>对象：是能够看得到摸的着的真实存在的实体</li>
<li>简单理解：<strong>类是对事物的一种描述，对象则为具体存在的事物</strong></li>
</ul>

        <h1 id="3-类的定义"   >
          <a href="#3-类的定义" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-类的定义" class="headerlink" title="3.类的定义"></a>3.类的定义</h1>
      <p>类的组成是由属性和行为两部分组成</p>
<ul>
<li><strong>属性</strong>：在类中通过成员变量来体现（类中方法外的变量）</li>
<li><strong>行为</strong>：在类中通过成员方法来体现（和前面的方法相比去掉static关键字即可）</li>
</ul>
<p>类的定义步骤：</p>
<p>​	① 定义类</p>
<p>​	② 编写类的成员变量</p>
<p>​	③ 编写类的成员方法</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="comment">// 属性 : 姓名, 年龄</span></span><br><span class="line">    <span class="comment">// 成员变量: 跟之前定义变量的格式一样, 只不过位置发生了改变, 类中方法外</span></span><br><span class="line">    String name;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 行为 : 学习</span></span><br><span class="line">    <span class="comment">// 成员方法: 跟之前定义方法的格式一样, 只不过去掉了static关键字.</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">study</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;学习&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="4-对象的创建和使用"   >
          <a href="#4-对象的创建和使用" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-对象的创建和使用" class="headerlink" title="4.对象的创建和使用"></a>4.对象的创建和使用</h1>
      <ol>
<li>创建对象的格式：</li>
</ol>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">类名 对象名 = new 类名();</span><br></pre></td></tr></table></div></figure>

<ol start="2">
<li>调用成员的格式：</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">对象名.成员变量;</span><br><span class="line">对象名.成员方法();</span><br></pre></td></tr></table></div></figure>

<ol start="3">
<li>示例代码：</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.object1;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestStudent</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        创建对象的格式:</span></span><br><span class="line"><span class="comment">                类名 对象名 = new 类名();</span></span><br><span class="line"><span class="comment">        调用成员变量的格式:</span></span><br><span class="line"><span class="comment">                对象名.变量名</span></span><br><span class="line"><span class="comment">        调用成员方法的格式:</span></span><br><span class="line"><span class="comment">                对象名.方法名();</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 类名 对象名 = new 类名();</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">stu</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        <span class="comment">// 对象名.变量名</span></span><br><span class="line">        <span class="comment">// 默认初始化值</span></span><br><span class="line">        System.out.println(stu.name);  <span class="comment">// null</span></span><br><span class="line">        System.out.println(stu.age);   <span class="comment">// 0</span></span><br><span class="line"></span><br><span class="line">        stu.name = <span class="string">&quot;张三&quot;</span>;</span><br><span class="line">        stu.age = <span class="number">23</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(stu.name);  <span class="comment">// 张三</span></span><br><span class="line">        System.out.println(stu.age);   <span class="comment">// 23</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 对象名.方法名();</span></span><br><span class="line">        stu.study();</span><br><span class="line">        <span class="comment">// com.itheima.object1.Student@b4c966a</span></span><br><span class="line">        <span class="comment">// 全类名(包名 + 类名)</span></span><br><span class="line">        System.out.println(stu);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="5-对象内存图"   >
          <a href="#5-对象内存图" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-对象内存图" class="headerlink" title="5.对象内存图"></a>5.对象内存图</h1>
      
        <h2 id="5-1-单个对象内存图"   >
          <a href="#5-1-单个对象内存图" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-单个对象内存图" class="headerlink" title="5.1 单个对象内存图"></a>5.1 单个对象内存图</h2>
      <p><img src="https://s2.loli.net/2022/02/13/Cy74QjdHtYDJbZM.png" alt="1590938666222"></p>

        <h2 id="5-2-多个对象内存图"   >
          <a href="#5-2-多个对象内存图" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-2-多个对象内存图" class="headerlink" title="5.2 多个对象内存图"></a>5.2 多个对象内存图</h2>
      <p><img src="https://s2.loli.net/2022/02/13/fhTKsli39aycxXF.png" alt="1590938693756"></p>
<p>总结：</p>
<p>  多个对象在堆内存中，都有不同的内存划分，成员变量存储在各自的内存区域中，成员方法多个对象共用的一份</p>

        <h2 id="5-3-多个对象指向相同内存图"   >
          <a href="#5-3-多个对象指向相同内存图" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-3-多个对象指向相同内存图" class="headerlink" title="5.3 多个对象指向相同内存图"></a>5.3 多个对象指向相同内存图</h2>
      <p><img src="https://s2.loli.net/2022/02/13/kQcj69B7fEXNHDO.png" alt="1590938711726"></p>
<p>总结 : </p>
<ul>
<li><p>当多个对象的引用指向同一个内存空间（变量所记录的地址值是一样的）</p>
</li>
<li><p>只要有任何一个对象修改了内存中的数据，随后，无论使用哪一个对象进行数据获取，都是修改后的数据。</p>
</li>
</ul>

        <h1 id="6-成员变量和局部变量的区别"   >
          <a href="#6-成员变量和局部变量的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-成员变量和局部变量的区别" class="headerlink" title="6.成员变量和局部变量的区别"></a>6.成员变量和局部变量的区别</h1>
      <ol>
<li><strong>类中位置不同</strong>：成员变量（类中方法外）局部变量（方法内部或方法声明上）</li>
<li><strong>内存中位置不同</strong>：成员变量（堆内存）局部变量（栈内存）</li>
<li><strong>生命周期不同</strong>：成员变量（随着对象的存在而存在，随着对象的消失而消失）局部变量（随着方法的调用而存在，醉着方法的调用完毕而消失）</li>
<li><strong>初始化值不同</strong>：成员变量（有默认初始化值）局部变量（没有默认初始化值，必须先定义，赋值才能使用）</li>
</ol>

        <h1 id="7-封装"   >
          <a href="#7-封装" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-封装" class="headerlink" title="7.封装"></a>7.封装</h1>
      <ul>
<li>封装概述<br> 是面向对象三大特征之一（封装，继承，多态）<br> 是面向对象编程语言对客观世界的模拟，客观世界里成员变量都是隐藏在对象内部的，外界是无法直接操作的</li>
<li>封装原则<br> 将类的某些信息隐藏在类内部，不允许外部程序直接访问，而是通过该类提供的方法来实现对隐藏信息的操作和访问<br> 成员变量private，提供对应的getXxx()&#x2F;setXxx()方法</li>
<li>封装好处<br> 通过方法来控制成员变量的操作，提高了代码的安全性<br> 把代码用方法进行封装，提高了代码的复用性</li>
</ul>

        <h2 id="7-1-private关键字"   >
          <a href="#7-1-private关键字" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-1-private关键字" class="headerlink" title="7.1 private关键字"></a>7.1 private关键字</h2>
      <ol>
<li>​	概述 : private是一个修饰符，可以用来修饰成员（成员变量，成员方法）</li>
<li>​	特点 : <ul>
<li>被private修饰的成员，只能在本类进行访问，针对private修饰的成员变量，如果需要被其他类使用，提供相应的操作</li>
<li>提供“get变量名()”方法，用于获取成员变量的值，方法用public修饰</li>
<li>提供“set变量名(参数)”方法，用于设置成员变量的值，方法用public修饰</li>
</ul>
</li>
<li>​	示例代码：</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    学生类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="comment">//成员变量</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//get/set方法</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String n)</span> &#123;</span><br><span class="line">        name = n;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">        age = a;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;,&quot;</span> + age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    学生测试类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建对象</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用set方法给成员变量赋值</span></span><br><span class="line">        s.setName(<span class="string">&quot;林青霞&quot;</span>);</span><br><span class="line">        s.setAge(<span class="number">30</span>);</span><br><span class="line"></span><br><span class="line">        s.show();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用get方法获取成员变量的值</span></span><br><span class="line">        System.out.println(s.getName() + <span class="string">&quot;---&quot;</span> + s.getAge());</span><br><span class="line">        System.out.println(s.getName() + <span class="string">&quot;,&quot;</span> + s.getAge());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="7-2-this关键字"   >
          <a href="#7-2-this关键字" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-2-this关键字" class="headerlink" title="7.2 this关键字"></a>7.2 this关键字</h2>
      <ol>
<li><p>概述 : this修饰的变量用于指代成员变量，其主要作用是（区分局部变量和成员变量的重名问题）</p>
</li>
<li><p>注意：</p>
<p>-<br>  方法的形参如果与成员变量同名，不带this修饰的变量指的是形参，而不是成员变量</p>
<p>-<br>  方法的形参没有与成员变量同名，不带this修饰的变量指的是成员变量</p>
</li>
<li><p>代码实现 :</p>
</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;,&quot;</span> + age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="7-2-1-this内存原理"   >
          <a href="#7-2-1-this内存原理" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-2-1-this内存原理" class="headerlink" title="7.2.1 this内存原理"></a>7.2.1 this内存原理</h3>
      <p>注意 : this代表当前调用方法的引用，哪个对象调用的方法，this就代表哪一个对象</p>
<p>图解 ：</p>
<p><img src="https://s2.loli.net/2022/02/13/f61ZJGP9DN8KxQk.png" alt="1590938942838"></p>
<p><img src="https://s2.loli.net/2022/02/13/Jxd3soQqBCI2Ruy.png" alt="1590938969305"></p>

        <h1 id="8-构造方法"   >
          <a href="#8-构造方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-构造方法" class="headerlink" title="8.构造方法"></a>8.构造方法</h1>
      
        <h2 id="8-1-注意"   >
          <a href="#8-1-注意" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-注意" class="headerlink" title="8.1 注意"></a>8.1 注意</h2>
      <ol>
<li>方法名与类名相同，大小写也要一致</li>
<li>没有返回值类型，连void都没有</li>
<li>没有具体的返回值（不能由retrun带回结果数据）</li>
</ol>

        <h2 id="8-2-执行时机"   >
          <a href="#8-2-执行时机" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-执行时机" class="headerlink" title="8.2 执行时机"></a>8.2 执行时机</h2>
      <ul>
<li>创建对象的时候调用，每创建一次对象，就会执行一次构造方法</li>
<li>不能手动调用构造方法</li>
</ul>

        <h2 id="8-3-作用"   >
          <a href="#8-3-作用" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-作用" class="headerlink" title="8.3 作用"></a>8.3 作用</h2>
      <p>​	用于给对象的数据（属性）进行初始化</p>

        <h2 id="8-4-构造方法的注意事项"   >
          <a href="#8-4-构造方法的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-4-构造方法的注意事项" class="headerlink" title="8.4 构造方法的注意事项"></a>8.4 构造方法的注意事项</h2>
      <ul>
<li><p>构造方法的创建 : </p>
<p>​	如果没有定义构造方法，系统将给出一个默认的无参数构造方法</p>
<p>​	如果定义了构造方法，系统将不再提供默认的构造方法</p>
</li>
<li><p>推荐的使用方式 : </p>
<p>​	无论是否使用，都手动书写无参数构造方法，和带参数构造方法</p>
</li>
</ul>
<p>	</p>

        <h1 id="9-一个标准的类案例"   >
          <a href="#9-一个标准的类案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-一个标准的类案例" class="headerlink" title="9.一个标准的类案例"></a>9.一个标准的类案例</h1>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.test3;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    JavaBean类: 封装数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span> &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(name + <span class="string">&quot;...&quot;</span> + age);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.test3;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestStudent</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 1. 无参数构造方法创建对象, 通过setXxx方法给成员变量进行赋值</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">stu1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">        stu1.setName(<span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        stu1.setAge(<span class="number">23</span>);</span><br><span class="line">        stu1.show();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2. 通过带参数构造方法, 直接给属性进行赋值</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">stu2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;李四&quot;</span>,<span class="number">24</span>);</span><br><span class="line">        stu2.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h1 id="10-static关键字"   >
          <a href="#10-static关键字" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-static关键字" class="headerlink" title="10.static关键字"></a>10.static关键字</h1>
      <p>static 关键字是静态的意思,是Java中的一个修饰符,可以修饰成员方法,成员变量</p>

        <h2 id="10-1-static修饰的特点"   >
          <a href="#10-1-static修饰的特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-1-static修饰的特点" class="headerlink" title="10.1 static修饰的特点"></a>10.1 static修饰的特点</h2>
      <ul>
<li><p>被类的所有对象共享</p>
<p>是我们判断是否使用静态关键字的条件</p>
</li>
<li><p>随着类的加载而加载，优先于对象存在</p>
<p>对象需要类被加载后，才能创建</p>
</li>
<li><p>可以通过类名调用</p>
<p>也可以通过对象名调用</p>
</li>
</ul>

        <h2 id="10-2-static关键字注意事项"   >
          <a href="#10-2-static关键字注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-2-static关键字注意事项" class="headerlink" title="10.2 static关键字注意事项"></a>10.2 static关键字注意事项</h2>
      <ul>
<li>静态方法只能访问静态的成员</li>
<li>非静态方法可以访问静态的成员，也可以访问非静态的成员</li>
<li>静态方法中是没有this关键字</li>
</ul>

        <h1 id="11-继承"   >
          <a href="#11-继承" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-继承" class="headerlink" title="11. 继承"></a>11. 继承</h1>
      <p>继承是面向对象三大特征之一，可以使得子类具有父类的属性和方法，还可以在子类中重新定义，以及追加属性和方法</p>

        <h2 id="11-1-继承的实现"   >
          <a href="#11-1-继承的实现" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-1-继承的实现" class="headerlink" title="11.1 继承的实现"></a>11.1 继承的实现</h2>
      <p>格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">class 子类 extends 父类 &#123; &#125; </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123; &#125;</span><br></pre></td></tr></table></div></figure>

<p>继承的作用：继承可以让类与类之间产生关系，子父类关系，产生子父类后，子类则可以使用父类中非私有的成员。</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;show方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Zi</span> <span class="keyword">extends</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;method方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建对象，调用方法</span></span><br><span class="line">        <span class="type">Fu</span> <span class="variable">f</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Fu</span>();</span><br><span class="line">        f.show();</span><br><span class="line"></span><br><span class="line">        <span class="type">Zi</span> <span class="variable">z</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Zi</span>();</span><br><span class="line">        z.method();</span><br><span class="line">        z.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="11-2-继承的好处和弊端"   >
          <a href="#11-2-继承的好处和弊端" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-2-继承的好处和弊端" class="headerlink" title="11.2 继承的好处和弊端"></a>11.2 继承的好处和弊端</h2>
      <ul>
<li>好处<ul>
<li>提高了代码的复用性(多个类相同的成员可以放到同一个类中)</li>
<li>提高了代码的维护性(如果方法的代码需要修改，修改一处即可)</li>
</ul>
</li>
<li>弊端<ul>
<li>继承让类与类之间产生了关系，类的耦合性增强了，当父类发生变化时子类实现也不得不跟着变化，削弱了子类的独立性</li>
</ul>
</li>
<li>应用场景：<ul>
<li>使用继承，需要考虑类与类之间是否存在is..a的关系，不能盲目使用继承</li>
<li>is..a的关系：谁是谁的一种，例如：老师和学生是人的一种，那人就是父类，学生和老师就是子类</li>
</ul>
</li>
</ul>

        <h2 id="11-3-继承的特点"   >
          <a href="#11-3-继承的特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-3-继承的特点" class="headerlink" title="11.3 继承的特点"></a>11.3 继承的特点</h2>
      <p>Java中继承的特点:</p>
<ol>
<li>Java中类只支持单继承，不支持多继承。错误范例：<code>class A extends B, C &#123; &#125;</code></li>
<li>Java中类支持多层继承</li>
</ol>
<p>多层继承示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Granddad</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">drink</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;爷爷爱喝酒&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Father</span> <span class="keyword">extends</span> <span class="title class_">Granddad</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">smoke</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;爸爸爱抽烟&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Mother</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">dance</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;妈妈爱跳舞&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Son</span> <span class="keyword">extends</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">	<span class="comment">// 此时，Son类中就同时拥有drink方法以及smoke方法</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="11-4-继承中的成员访问特点"   >
          <a href="#11-4-继承中的成员访问特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-继承中的成员访问特点" class="headerlink" title="11.4 继承中的成员访问特点"></a>11.4 继承中的成员访问特点</h2>
      
        <h3 id="11-4-1-继承中变量的访问特点"   >
          <a href="#11-4-1-继承中变量的访问特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-1-继承中变量的访问特点" class="headerlink" title="11.4.1 继承中变量的访问特点"></a>11.4.1 继承中变量的访问特点</h3>
      <p>在子类方法中访问一个变量，采用的是就近原则。</p>
<p>子类局部范围找—— &gt;子类成员范围找—— &gt;父类成员范围找—— &gt;如果都没有就报错(不考虑父亲的父亲…)</p>
<p>示例代码:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Zi</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line">        System.out.println(num);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Zi</span> <span class="variable">z</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Zi</span>();</span><br><span class="line">        z.show();	<span class="comment">// 输出show方法中的局部变量30</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="11-4-2-super"   >
          <a href="#11-4-2-super" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-2-super" class="headerlink" title="11.4.2 super"></a>11.4.2 super</h3>
      <ul>
<li>this&amp;super关键字：<ul>
<li>this：代表本类对象的引用</li>
<li>super：代表父类存储空间的标识(可以理解为父类对象引用)</li>
</ul>
</li>
<li>this和super的使用分别<ul>
<li>成员变量：<ul>
<li>this.成员变量    -   访问本类成员变量</li>
<li>super.成员变量 -   访问父类成员变量</li>
</ul>
</li>
<li>成员方法：<ul>
<li>this.成员方法  - 访问本类成员方法</li>
<li>super.成员方法 - 访问父类成员方法</li>
</ul>
</li>
</ul>
</li>
<li>构造方法：<ul>
<li>this(…)  -  访问本类构造方法</li>
<li>super(…)  -  访问父类构造方法</li>
</ul>
</li>
</ul>

        <h3 id="11-4-3-继承中构造方法的访问特点"   >
          <a href="#11-4-3-继承中构造方法的访问特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-3-继承中构造方法的访问特点" class="headerlink" title="11.4.3 继承中构造方法的访问特点"></a>11.4.3 继承中构造方法的访问特点</h3>
      <p><strong>注意：子类中所有的构造方法默认都会访问父类中无参的构造方法</strong></p>
<p>​	子类会继承父类中的数据，可能还会使用父类的数据。所以，子类初始化之前，一定要先完成父类数据的初始化，原因在于，每一个子类构造方法的第一条语句默认都是：super()</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">问题：如果父类中没有无参构造方法，只有带参构造方法，该怎么办呢？</span><br><span class="line"><span class="number">1.</span> 通过使用<span class="built_in">super</span>关键字去显式的调用父类的带参构造方法	<span class="built_in">super</span>(name,age);</span><br><span class="line"><span class="number">2.</span> 子类通过<span class="built_in">this</span>去调用本类的其他构造方法,本类其他构造方法再通过<span class="built_in">super</span>去手动调用父类的带参的构造方法</span><br><span class="line"></span><br><span class="line">注意: <span class="built_in">this</span>(…)<span class="built_in">super</span>(…) 必须放在构造方法的第一行有效语句，并且二者不能共存</span><br></pre></td></tr></table></div></figure>


        <h3 id="11-4-4-继承中成员方法的访问特点"   >
          <a href="#11-4-4-继承中成员方法的访问特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-4-继承中成员方法的访问特点" class="headerlink" title="11.4.4 继承中成员方法的访问特点"></a>11.4.4 继承中成员方法的访问特点</h3>
      <p>通过子类对象访问一个方法</p>
<p>子类成员范围找—— &gt;父类成员范围找—— &gt;如果都没有就报错(不考虑父亲的父亲…)</p>

        <h3 id="11-4-5-super内存图"   >
          <a href="#11-4-5-super内存图" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-5-super内存图" class="headerlink" title="11.4.5 super内存图"></a>11.4.5 super内存图</h3>
      <p>对象在堆内存中，会单独存在一块super区域，用来存放父类的数据 </p>
<p><img src="https://s2.loli.net/2022/02/14/nEaL6QmevAoUjY7.png" alt="01_super内存图"></p>

        <h3 id="11-4-6-方法重写"   >
          <a href="#11-4-6-方法重写" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-6-方法重写" class="headerlink" title="11.4.6 方法重写"></a>11.4.6 方法重写</h3>
      <p>1、方法重写概念：子类出现了和父类中一模一样的方法声明（方法名一样，参数列表也必须一样）</p>
<p>2、方法重写的应用场景：当子类需要父类的功能，而功能主体子类有自己特有内容时，可以重写父类中的方法，这样，即沿袭了父类的功能，又定义了子类特有的内容</p>
<p>3、Override注解：用来检测当前的方法，是否是重写的方法，起到【校验】的作用</p>

        <h4 id="11-4-6-1-注意事项"   >
          <a href="#11-4-6-1-注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-6-1-注意事项" class="headerlink" title="11.4.6.1 注意事项"></a>11.4.6.1 注意事项</h4>
      <ol>
<li>私有方法不能被重写(父类私有成员子类是不能继承的)</li>
<li>子类方法访问权限不能更低(public &gt; 默认 &gt; 私有)</li>
<li>静态方法不能被重写,如果子类也有相同的方法,并不是重写的父类的方法</li>
</ol>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Fu中show()方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Fu中method()方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Zi</span> <span class="keyword">extends</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 编译【出错】，子类不能重写父类私有的方法*/</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Zi中show()方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    <span class="comment">/* 编译【出错】，子类重写父类方法的时候，访问权限需要大于等于父类 */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Zi中method()方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 编译【通过】，子类重写父类方法的时候，访问权限需要大于等于父类 */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Zi中method()方法被调用&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h4 id="11-4-6-2-权限修饰符"   >
          <a href="#11-4-6-2-权限修饰符" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-4-6-2-权限修饰符" class="headerlink" title="11.4.6.2 权限修饰符"></a>11.4.6.2 权限修饰符</h4>
      <div class="table-container"><table>
<thead>
<tr>
<th align="center">修饰符</th>
<th align="center">同一个类中</th>
<th align="center">同一个包中子类无关类</th>
<th align="center">不同包的子类</th>
<th align="center">不同包的无关类</th>
</tr>
</thead>
<tbody><tr>
<td align="center">private</td>
<td align="center">可以</td>
<td align="center"></td>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center">默认</td>
<td align="center">可以</td>
<td align="center">可以</td>
<td align="center"></td>
<td align="center"></td>
</tr>
<tr>
<td align="center">protected</td>
<td align="center">可以</td>
<td align="center">可以</td>
<td align="center">可以</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">public</td>
<td align="center">可以</td>
<td align="center">可以</td>
<td align="center">可以</td>
<td align="center">可以</td>
</tr>
</tbody></table></div>

        <h1 id="12-抽象类"   >
          <a href="#12-抽象类" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-抽象类" class="headerlink" title="12. 抽象类"></a>12. 抽象类</h1>
      <p>​	当我们在做子类共性功能抽取时，有些方法在父类中并没有具体的体现，这个时候就需要抽象类了。</p>
<p>​	在Java中，一个没有方法体的方法应该定义为抽象方法，而&#x3D;&#x3D;类中如果有抽象方法，该类必须定义为抽象类！&#x3D;&#x3D;</p>

        <h2 id="12-1-抽象类的特点"   >
          <a href="#12-1-抽象类的特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-1-抽象类的特点" class="headerlink" title="12.1 抽象类的特点"></a>12.1 抽象类的特点</h2>
      <p>抽象类和抽象方法必须使用 abstract 关键字修饰：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//抽象类的定义</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> class 类名 &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//抽象方法的定义</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span>;</span><br></pre></td></tr></table></div></figure>

<p>注意：</p>
<ul>
<li><p>抽象类中不一定有抽象方法，有抽象方法的类一定是抽象类</p>
</li>
<li><p>抽象类不能实例化</p>
</li>
<li><p>抽象类可以有构造方法</p>
</li>
<li><p>抽象类的子类</p>
<p>​	要么重写抽象类中的所有抽象方法</p>
<p>​	要么是抽象类</p>
</li>
</ul>

        <h2 id="12-2-抽象类的案例"   >
          <a href="#12-2-抽象类的案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-2-抽象类的案例" class="headerlink" title="12.2 抽象类的案例"></a>12.2 抽象类的案例</h2>
      <ul>
<li><p>案例需求</p>
<p>​	定义猫类(Cat)和狗类(Dog)</p>
<p>​	猫类成员方法：eat（猫吃鱼）drink（喝水…）</p>
<p>​	狗类成员方法：eat（狗吃肉）drink（喝水…）</p>
</li>
<li><p>实现步骤</p>
<ol>
<li>猫类和狗类中存在共性内容，应向上抽取出一个动物类（Animal）</li>
<li>父类Animal中，无法将 eat 方法具体实现描述清楚，所以定义为抽象方法</li>
<li>抽象方法需要存活在抽象类中，将Animal定义为抽象类</li>
<li>让 Cat 和 Dog 分别继承 Animal，重写eat方法</li>
<li>测试类中创建 Cat 和 Dog 对象，调用方法测试</li>
</ol>
</li>
<li><p>代码实现</p>
<ul>
<li>动物类</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">drink</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;喝水&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Animal</span><span class="params">()</span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>猫类</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Cat</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫吃鱼&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>狗类</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;狗吃肉&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>测试类</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Dog</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();</span><br><span class="line">        d.eat();<span class="comment">//狗吃肉</span></span><br><span class="line">        d.drink();<span class="comment">//喝水</span></span><br><span class="line"></span><br><span class="line">        <span class="type">Cat</span> <span class="variable">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Cat</span>();</span><br><span class="line">        c.drink();<span class="comment">//喝水</span></span><br><span class="line">        c.eat();<span class="comment">//猫吃鱼</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//Animal a = new Animal();//报错：Animal是抽象的; 无法实例化</span></span><br><span class="line">        <span class="comment">//a.eat();</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="12-3-final"   >
          <a href="#12-3-final" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-3-final" class="headerlink" title="12.3 final"></a>12.3 final</h2>
      
        <h3 id="12-3-1-fianl关键字的作用"   >
          <a href="#12-3-1-fianl关键字的作用" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-3-1-fianl关键字的作用" class="headerlink" title="12.3.1 fianl关键字的作用"></a>12.3.1 fianl关键字的作用</h3>
      <p>final代表最终的意思，可以修饰成员方法，成员变量，类</p>

        <h3 id="12-3-2-final修饰类、方法、变量的效果"   >
          <a href="#12-3-2-final修饰类、方法、变量的效果" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-3-2-final修饰类、方法、变量的效果" class="headerlink" title="12.3.2 final修饰类、方法、变量的效果"></a>12.3.2 final修饰类、方法、变量的效果</h3>
      <ul>
<li><p>fianl修饰类：该类不能被继承（不能有子类，但是可以有父类）</p>
</li>
<li><p>final修饰方法：该方法不能被重写</p>
</li>
<li><p>final修饰变量：表明该变量是一个常量，不能再次赋值</p>
<ul>
<li><p>变量是基本类型,不能改变的是值</p>
</li>
<li><p>变量是引用类型,不能改变的是地址值,但地址里面的内容是可以改变的</p>
</li>
<li><p>举例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Student</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="number">23</span>);</span><br><span class="line">  	s = <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="number">24</span>);  <span class="comment">// 错误</span></span><br><span class="line"> 	s.setAge(<span class="number">24</span>);  <span class="comment">// 正确</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
</ul>

        <h1 id="13-代码块"   >
          <a href="#13-代码块" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-代码块" class="headerlink" title="13.代码块"></a>13.代码块</h1>
      <p>在Java中，使用 { } 括起来的代码被称为代码块</p>

        <h2 id="13-1-局部代码块"   >
          <a href="#13-1-局部代码块" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-1-局部代码块" class="headerlink" title="13.1 局部代码块"></a>13.1 局部代码块</h2>
      <p>位置: 方法中定义</p>
<p>作用: 限定变量的生命周期，及早释放，提高内存利用率</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        局部代码块</span></span><br><span class="line"><span class="comment">            位置：方法中定义</span></span><br><span class="line"><span class="comment">            作用：限定变量的生命周期，及早释放，提高内存利用率</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">            System.out.println(a);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">       <span class="comment">// System.out.println(a);</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="13-2-构造代码块"   >
          <a href="#13-2-构造代码块" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-2-构造代码块" class="headerlink" title="13.2 构造代码块"></a>13.2 构造代码块</h2>
      <p>位置: 类中方法外定义</p>
<p>特点: 每次构造方法执行的时，都会执行该代码块中的代码，并且在构造方法执行前执行</p>
<p>作用: 将多个构造方法中相同的代码，抽取到构造代码块中，提高代码的复用性</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        构造代码块:</span></span><br><span class="line"><span class="comment">            位置：类中方法外定义</span></span><br><span class="line"><span class="comment">            特点：每次构造方法执行的时，都会执行该代码块中的代码，并且在构造方法执行前执行</span></span><br><span class="line"><span class="comment">            作用：将多个构造方法中相同的代码，抽取到构造代码块中，提高代码的复用性</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">stu1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>();</span><br><span class="line">      	<span class="comment">//好好学习	</span></span><br><span class="line">      	<span class="comment">//空参数构造方法</span></span><br><span class="line">        <span class="type">Student</span> <span class="variable">stu2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="number">10</span>);</span><br><span class="line">      	<span class="comment">//好好学习	</span></span><br><span class="line">      	<span class="comment">//带参数构造方法...........</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;好好学习&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;空参数构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(<span class="type">int</span> a)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;带参数构造方法...........&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="13-3-静态代码块"   >
          <a href="#13-3-静态代码块" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-3-静态代码块" class="headerlink" title="13.3 静态代码块"></a>13.3 静态代码块</h2>
      <p>位置: 类中方法外定义</p>
<p>特点: 需要通过static关键字修饰，随着类的加载而加载，并且只执行一次</p>
<p>作用: 在类加载的时候做一些数据初始化的操作</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        静态代码块:</span></span><br><span class="line"><span class="comment">            位置：类中方法外定义</span></span><br><span class="line"><span class="comment">            特点：需要通过static关键字修饰，随着类的加载而加载，并且只执行一次</span></span><br><span class="line"><span class="comment">            作用：在类加载的时候做一些数据初始化的操作</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Person</span> <span class="variable">p1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>();</span><br><span class="line">      	<span class="comment">//我是静态代码块, 我执行了</span></span><br><span class="line">      	<span class="comment">//我是Person类的空参数构造方法</span></span><br><span class="line">        <span class="type">Person</span> <span class="variable">p2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="number">10</span>);</span><br><span class="line">      	<span class="comment">//我是Person类的带...........参数构造方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;我是静态代码块, 我执行了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;我是Person类的空参数构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(<span class="type">int</span> a)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;我是Person类的带...........参数构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="14-接口"   >
          <a href="#14-接口" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-接口" class="headerlink" title="14.接口"></a>14.接口</h1>
      <p>接口就是一种公共的规范标准，只要符合规范标准，大家都可以通用。</p>
<p>Java中接口存在的两个意义：用来定义规范、用来做功能的拓展</p>

        <h2 id="14-1-特点"   >
          <a href="#14-1-特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-1-特点" class="headerlink" title="14.1 特点"></a>14.1 特点</h2>
      <p>接口用关键字interface修饰</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> interface 接口名 &#123;&#125; </span><br></pre></td></tr></table></div></figure>

<p>类实现接口用implements表示</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> class 类名 implements 接口名 &#123;&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="14-2-注意"   >
          <a href="#14-2-注意" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-2-注意" class="headerlink" title="14.2 注意"></a>14.2 注意</h2>
      <ul>
<li><p>接口不能实例化</p>
<p>​	我们可以创建接口的实现类对象使用</p>
</li>
<li><p>接口的子类</p>
<p>​	要么重写接口中的所有抽象方法</p>
<p>​	要么子类也是抽象类</p>
</li>
</ul>

        <h2 id="14-3-成员特点"   >
          <a href="#14-3-成员特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-3-成员特点" class="headerlink" title="14.3 成员特点"></a>14.3 成员特点</h2>
      <ul>
<li><p>成员变量</p>
<p>​	 只能是常量<br>​	 默认修饰符：<code>public static final</code></p>
</li>
<li><p>构造方法</p>
<p>​	没有，因为接口主要是扩展功能的，而没有具体存在</p>
</li>
<li><p>成员方法</p>
<p>​	只能是抽象方法</p>
<p>​	默认修饰符：<code>public abstract</code></p>
<p>​	关于接口中的方法，JDK8和JDK9中有一些新特性，后面再讲解</p>
</li>
</ul>
<p>代码演示:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Inter</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">NUM</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实现类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">InterImpl</span> <span class="keyword">implements</span> <span class="title class_">Inter</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="comment">// NUM = 20;</span></span><br><span class="line">        System.out.println(NUM);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//测试类</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestInterface</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        成员变量: 只能是常量 系统会默认加入三个关键字</span></span><br><span class="line"><span class="comment">                    public static final</span></span><br><span class="line"><span class="comment">        构造方法: 没有</span></span><br><span class="line"><span class="comment">        成员方法: 只能是抽象方法, 系统会默认加入两个关键字</span></span><br><span class="line"><span class="comment">                    public abstract</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(Inter.NUM);</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="14-4-类和接口的关系"   >
          <a href="#14-4-类和接口的关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-4-类和接口的关系" class="headerlink" title="14.4 类和接口的关系"></a>14.4 类和接口的关系</h2>
      <ul>
<li><p>类与类的关系</p>
<p>​	继承关系，只能单继承，不能多继承，但是可以多层继承</p>
</li>
<li><p>类与接口的关系</p>
<p>​	实现关系，可以单实现，也可以多实现，还可以在继承一个类的同时实现多个接口</p>
</li>
<li><p>接口与接口的关系</p>
<p>​	继承关系，可以单继承，也可以多继承</p>
</li>
</ul>

        <h2 id="14-5-接口组成更新"   >
          <a href="#14-5-接口组成更新" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-5-接口组成更新" class="headerlink" title="14.5 接口组成更新"></a>14.5 接口组成更新</h2>
      <ul>
<li><p>常量</p>
<p><code>public static final</code></p>
</li>
<li><p>抽象方法</p>
<p><code>public abstract</code></p>
</li>
<li><p>默认方法(Java 8)</p>
</li>
<li><p>静态方法(Java 8)</p>
</li>
<li><p>私有方法(Java 9)</p>
</li>
</ul>

        <h3 id="14-5-1-接口中默认方法"   >
          <a href="#14-5-1-接口中默认方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-5-1-接口中默认方法" class="headerlink" title="14.5.1 接口中默认方法"></a>14.5.1 接口中默认方法</h3>
      <p>作用：解决接口升级的问题</p>
<p>格式:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">default</span> 返回值类型 方法名(参数列表) &#123;   &#125;</span><br></pre></td></tr></table></div></figure>

<p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">show3</span><span class="params">()</span> &#123; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>注意事项：</p>
<ul>
<li>默认方法不是抽象方法，所以不强制被重写。但是可以被重写，重写的时候去掉default关键字</li>
<li>public可以省略，default不能省略</li>
<li>如果实现了多个接口，多个接口中存在相同的方法声明，子类就必须对该方法进行重写</li>
</ul>

        <h3 id="14-5-2-接口中静态方法"   >
          <a href="#14-5-2-接口中静态方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-5-2-接口中静态方法" class="headerlink" title="14.5.2 接口中静态方法"></a>14.5.2 接口中静态方法</h3>
      <p>格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> 返回值类型 方法名(参数列表) &#123;   &#125;</span><br></pre></td></tr></table></div></figure>

<p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>注意事项：</p>
<ul>
<li>静态方法只能通过接口名调用，不能通过实现类名或者对象名调用</li>
<li>public可以省略，static不能省略</li>
</ul>

        <h3 id="14-5-3-接口中私有方法"   >
          <a href="#14-5-3-接口中私有方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-5-3-接口中私有方法" class="headerlink" title="14.5.3 接口中私有方法"></a>14.5.3 接口中私有方法</h3>
      <p>私有方法产生原因：</p>
<p>Java 9中新增了带方法体的私有方法，这其实在Java 8中就埋下了伏笔：Java 8允许在接口中定义带方法体的默认方法和静态方法。这样可能就会引发一个问题：当两个默认方法或者静态方法中包含一段相同的代码实现时，程序必然考虑将这段实现代码抽取成一个共性方法，而这个共性方法是不需要让别人使用的，因此用私有给隐藏起来，这就是Java 9增加私有方法的必然性</p>
<p>定义格式：</p>
<ul>
<li><p>格式1</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> 返回值类型 方法名(参数列表) &#123;   &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>格式2</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> 返回值类型 方法名(参数列表) &#123;   &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
<p>注意事项：</p>
<ul>
<li>默认方法可以调用私有的静态方法和非静态方法</li>
<li>静态方法只能调用私有的静态方法</li>
</ul>
<p>代码示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Inter</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">start</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;start方法执行了...&quot;</span>);</span><br><span class="line">        log();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">end</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;end方法执行了...&quot;</span>);</span><br><span class="line">        log();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">log</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;默认方法公用部分&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">check</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;静态方法共用部分&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">open</span><span class="params">()</span> &#123;</span><br><span class="line">        check();</span><br><span class="line">        System.out.println(<span class="string">&quot;open方法执行了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">close</span><span class="params">()</span>&#123;</span><br><span class="line">        check();</span><br><span class="line">        System.out.println(<span class="string">&quot;close方法执行了&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestInterface</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">InterImpl</span> <span class="variable">ii</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">InterImpl</span>();</span><br><span class="line">        ii.start();</span><br><span class="line">      	<span class="comment">//start方法执行了...</span></span><br><span class="line">      	<span class="comment">//默认方法公用部分</span></span><br><span class="line">        ii.end();</span><br><span class="line">      	<span class="comment">//end方法执行了...</span></span><br><span class="line">     	 	<span class="comment">//默认方法公用部分</span></span><br><span class="line"></span><br><span class="line">      	Inter.open();</span><br><span class="line">				<span class="comment">//静态方法共用部分</span></span><br><span class="line">				<span class="comment">//open方法执行了</span></span><br><span class="line">        Inter.close();</span><br><span class="line">        <span class="comment">//静态方法共用部分</span></span><br><span class="line">				<span class="comment">//close方法执行了</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">InterImpl</span> <span class="keyword">implements</span> <span class="title class_">Inter</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="15-多态"   >
          <a href="#15-多态" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-多态" class="headerlink" title="15.多态"></a>15.多态</h1>
      <p>​	同一个对象，在不同时刻表现出来的不同形态</p>

        <h2 id="15-1-概述"   >
          <a href="#15-1-概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-1-概述" class="headerlink" title="15.1 概述"></a>15.1 概述</h2>
      <p>多态的前提：</p>
<ol>
<li>要有继承或实现关系</li>
<li>要有方法的重写</li>
<li>要有父类引用指向子类对象</li>
</ol>
<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;动物吃饭&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cat</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫吃鱼&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1Polymorphic</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        多态的前提:</span></span><br><span class="line"><span class="comment">            1. 要有(继承 \ 实现)关系</span></span><br><span class="line"><span class="comment">            2. 要有方法重写</span></span><br><span class="line"><span class="comment">            3. 要有父类引用, 指向子类对象</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 当前事物, 是一只猫</span></span><br><span class="line">        <span class="type">Cat</span> <span class="variable">c</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Cat</span>();</span><br><span class="line">        <span class="comment">// 当前事物, 是一只动物</span></span><br><span class="line">        <span class="type">Animal</span> <span class="variable">a</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Cat</span>();</span><br><span class="line">        a.eat();</span><br><span class="line">      	<span class="comment">//猫吃鱼</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="15-2-多态中的成员访问特点"   >
          <a href="#15-2-多态中的成员访问特点" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-2-多态中的成员访问特点" class="headerlink" title="15.2 多态中的成员访问特点"></a>15.2 多态中的成员访问特点</h2>
      <p>成员访问特点：</p>
<ul>
<li><p>成员变量</p>
<p>​	编译看父类，运行看父类</p>
</li>
<li><p>成员方法</p>
<p>​	&#x3D;&#x3D;编译看父类，运行看子类&#x3D;&#x3D;</p>
</li>
</ul>
<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Fu.. method&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Zi</span> <span class="keyword">extends</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Zi.. method&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test2Polymorpic</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">         多态的成员访问特点:</span></span><br><span class="line"><span class="comment">                成员变量: 编译看左边 (父类), 运行看左边 (父类)</span></span><br><span class="line"><span class="comment">                成员方法: 编译看左边 (父类), 运行看右边 (子类)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Fu</span> <span class="variable">f</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Zi</span>();</span><br><span class="line">        System.out.println(f.num);<span class="comment">//10</span></span><br><span class="line">        f.method();<span class="comment">//Zi.. method</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="15-3-多态的好处和弊端"   >
          <a href="#15-3-多态的好处和弊端" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-3-多态的好处和弊端" class="headerlink" title="15.3 多态的好处和弊端"></a>15.3 多态的好处和弊端</h2>
      <ul>
<li><p>好处</p>
<p>​	提高程序的扩展性。定义方法时候，使用父类型作为参数，在使用的时候，使用具体的子类型参与操作</p>
</li>
<li><p>弊端</p>
<p>​	不能使用子类的特有成员</p>
</li>
</ul>

        <h2 id="15-4-多态中的转型"   >
          <a href="#15-4-多态中的转型" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-4-多态中的转型" class="headerlink" title="15.4 多态中的转型"></a>15.4 多态中的转型</h2>
      <ul>
<li><p>向上转型：父类引用指向子类对象就是向上转型</p>
</li>
<li><p>向下转型：<code>子类型 对象名 = (子类型)父类引用;</code></p>
</li>
</ul>
<p>代码演示：</p>
  <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Fu..show...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Zi</span> <span class="keyword">extends</span> <span class="title class_">Fu</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Zi..show...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;我是子类特有的方法, method&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test3Polymorpic</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 1. 向上转型 : 父类引用指向子类对象</span></span><br><span class="line">        <span class="type">Fu</span> <span class="variable">f</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Zi</span>();</span><br><span class="line">        f.show();</span><br><span class="line">        <span class="comment">// 多态的弊端: 不能调用子类特有的成员</span></span><br><span class="line">        <span class="comment">// f.method();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// A: 直接创建子类对象</span></span><br><span class="line">        <span class="comment">// B: 向下转型</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2. 向下转型 : 从父类类型, 转换回子类类型</span></span><br><span class="line">        <span class="type">Zi</span> <span class="variable">z</span> <span class="operator">=</span> (Zi) f;</span><br><span class="line">        z.method();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="15-5-转型存在的风险和解决方案"   >
          <a href="#15-5-转型存在的风险和解决方案" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-5-转型存在的风险和解决方案" class="headerlink" title="15.5 转型存在的风险和解决方案"></a>15.5 转型存在的风险和解决方案</h2>
      <p>风险：如果被转的引用类型变量,对应的实际类型和目标类型不是同一种类型,那么在转换的时候就会出现ClassCastException </p>
<p>解决方案：用<code>instanceof</code>先判断再操作</p>
<p>使用格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">变量名 <span class="keyword">instanceof</span> 类型</span><br></pre></td></tr></table></div></figure>

<p>通俗的理解：判断关键字左边的变量，是否是右边的类型，返回boolean类型结果</p>
<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;狗吃肉&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">watchHome</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;看家&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cat</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;猫吃鱼&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test4Polymorpic</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        useAnimal(<span class="keyword">new</span> <span class="title class_">Dog</span>());</span><br><span class="line">      	<span class="comment">//狗吃肉</span></span><br><span class="line">				<span class="comment">//看家</span></span><br><span class="line">        useAnimal(<span class="keyword">new</span> <span class="title class_">Cat</span>());</span><br><span class="line">      	<span class="comment">//猫吃鱼</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">useAnimal</span><span class="params">(Animal a)</span>&#123;  <span class="comment">// Animal a = new Dog();</span></span><br><span class="line">                                  <span class="comment">// Animal a = new Cat();</span></span><br><span class="line">        a.eat();</span><br><span class="line"><span class="comment">//      a.watchHome();//无法解析Animal中的方法watchHome</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//      Dog dog = (Dog) a;</span></span><br><span class="line"><span class="comment">//      dog.watchHome();// ClassCastException  类型转换异常</span></span><br><span class="line">      </span><br><span class="line">        <span class="comment">// 判断a变量记录的类型, 是否是Dog</span></span><br><span class="line">        <span class="keyword">if</span>(a <span class="keyword">instanceof</span> Dog)&#123;</span><br><span class="line">            <span class="type">Dog</span> <span class="variable">dog</span> <span class="operator">=</span> (Dog) a;<span class="comment">//必须强转，否则无法解析。。。</span></span><br><span class="line">            dog.watchHome();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="16-内部类"   >
          <a href="#16-内部类" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-内部类" class="headerlink" title="16.内部类"></a>16.内部类</h1>
      
        <h2 id="16-1-概述"   >
          <a href="#16-1-概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-1-概述" class="headerlink" title="16.1 概述"></a>16.1 概述</h2>
      <p>在一个类中定义一个类。举例：在一个类A的内部定义一个类B，类B就被称为内部类</p>
<p>内部类定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	格式：</span></span><br><span class="line"><span class="comment">    class 外部类名&#123;</span></span><br><span class="line"><span class="comment">    	修饰符 class 内部类名&#123;</span></span><br><span class="line"><span class="comment">    	</span></span><br><span class="line"><span class="comment">    	&#125;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Outer</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Inner</span> &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>内部类的访问特点 ：</p>
<ul>
<li>内部类可以直接访问外部类的成员，包括私有</li>
<li>外部类要访问内部类的成员，必须创建对象</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    内部类访问特点：</span></span><br><span class="line"><span class="comment">        内部类可以直接访问外部类的成员，包括私有</span></span><br><span class="line"><span class="comment">        外部类要访问内部类的成员，必须创建对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Outer</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">outerNum</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Inner</span> &#123;</span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">innerNum</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">            System.out.println(outerNum);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Inner</span> <span class="variable">i</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Inner</span>();</span><br><span class="line">        i.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Outer</span> <span class="variable">outer</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Outer</span>();</span><br><span class="line">        outer.method();<span class="comment">//10</span></span><br><span class="line"><span class="comment">//        outer.show();//找不到，报错</span></span><br><span class="line"><span class="comment">//        System.out.println(outer.innerNum);//找不到，报错</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="16-2-成员内部类"   >
          <a href="#16-2-成员内部类" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-2-成员内部类" class="headerlink" title="16.2 成员内部类"></a>16.2 成员内部类</h2>
      <p>成员内部类的定义位置：在类中方法，跟成员变量是一个位置</p>
<p>外界创建成员内部类格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">外部类名.内部类名 对象名 = 外部类对象.内部类对象;</span><br><span class="line"></span><br><span class="line">Outer.<span class="type">Inner</span> <span class="variable">oi</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Outer</span>().<span class="keyword">new</span> <span class="title class_">Inner</span>();</span><br></pre></td></tr></table></div></figure>


        <h3 id="16-2-1-私有成员内部类："   >
          <a href="#16-2-1-私有成员内部类：" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-2-1-私有成员内部类：" class="headerlink" title="16.2.1 私有成员内部类："></a>16.2.1 私有成员内部类：</h3>
      <p>将一个类，设计为内部类的目的，大多数都是不想让外界去访问，所以内部类的定义应该私有化，私有化之后，再提供一个可以让外界调用的方法，方法内部创建内部类对象并调用。</p>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Outer</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">class</span> <span class="title class_">Inner</span> &#123;</span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">            System.out.println(num);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Inner</span> <span class="variable">i</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Inner</span>();</span><br><span class="line">        i.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">InnerDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">      </span><br><span class="line">			<span class="comment">//Outer.Inner oi = new Outer().new Inner();</span></span><br><span class="line">			<span class="comment">//oi.show();</span></span><br><span class="line">      <span class="comment">//私有化后就不能这么写</span></span><br><span class="line">      </span><br><span class="line">        <span class="type">Outer</span> <span class="variable">o</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Outer</span>();</span><br><span class="line">        o.method();<span class="comment">//10</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="16-2-2-静态成员内部类"   >
          <a href="#16-2-2-静态成员内部类" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-2-2-静态成员内部类" class="headerlink" title="16.2.2 静态成员内部类"></a>16.2.2 静态成员内部类</h3>
      <p>静态成员内部类中的普通方法访问格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">外部类名.内部类名 对象名 = <span class="keyword">new</span> 外部类名.内部类名();</span><br></pre></td></tr></table></div></figure>

<p>静态成员内部类中的静态方法访问格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">外部类名.内部类名.方法名();</span><br></pre></td></tr></table></div></figure>

<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Outer</span> &#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">Inner</span> &#123;</span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;inner..show&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;inner..method&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test3Innerclass</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        静态成员内部类演示</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 外部类名.内部类名 对象名 = new 外部类名.内部类名();</span></span><br><span class="line">        Outer.<span class="type">Inner</span> <span class="variable">oi</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Outer</span>.Inner();</span><br><span class="line">        oi.show();</span><br><span class="line"></span><br><span class="line">        Outer.Inner.method();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="16-3-局部内部类"   >
          <a href="#16-3-局部内部类" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-3-局部内部类" class="headerlink" title="16.3 局部内部类"></a>16.3 局部内部类</h2>
      <p>局部内部类定义位置：在方法中定义的类</p>
<p>局部内部类方式方式</p>
<ul>
<li>局部内部类，外界是无法直接使用，需要在方法内部创建对象并使用</li>
<li>该类可以直接访问外部类的成员，也可以访问方法内的局部变量</li>
</ul>
<p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Outer</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">        <span class="keyword">class</span> <span class="title class_">Inner</span> &#123;</span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">                System.out.println(num);</span><br><span class="line">                System.out.println(num2);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">Inner</span> <span class="variable">i</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Inner</span>();</span><br><span class="line">        i.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OuterDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Outer</span> <span class="variable">o</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Outer</span>();</span><br><span class="line">        o.method();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="16-4-匿名内部类"   >
          <a href="#16-4-匿名内部类" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-4-匿名内部类" class="headerlink" title="16.4 匿名内部类"></a>16.4 匿名内部类</h2>
      <p>本质：匿名内部类是一个继承了该类或者实现了该接口的子类匿名对象,可以通过多态的形式接受</p>
<p>前提：存在一个类或者接口，这里的类可以是具体类也可以是抽象类</p>
<p>代码示例:</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">interface</span> <span class="title class_">Inter</span>&#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Test</span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Inter</span>()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;我是匿名内部类&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;.method();	<span class="comment">// 直接调用方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//匿名内部类在开发中的使用：当发现某个方法需要，接口或抽象类的子类对象，我们就可以传递一个匿名内部类过去，来简化传统的代码</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    游泳接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Swimming</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">swim</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestSwimming</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        goSwimming(<span class="keyword">new</span> <span class="title class_">Swimming</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">swim</span><span class="params">()</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;铁汁, 我们去游泳吧&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用接口的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">goSwimming</span><span class="params">(Swimming swimming)</span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            Swimming swim = new Swimming() &#123;</span></span><br><span class="line"><span class="comment">                @Override</span></span><br><span class="line"><span class="comment">                public void swim() &#123;</span></span><br><span class="line"><span class="comment">                    System.out.println(&quot;铁汁, 我们去游泳吧&quot;);</span></span><br><span class="line"><span class="comment">                &#125;</span></span><br><span class="line"><span class="comment">            &#125;</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        swimming.swim();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="17-Lambda表达式"   >
          <a href="#17-Lambda表达式" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-Lambda表达式" class="headerlink" title="17.Lambda表达式"></a>17.Lambda表达式</h1>
      
        <h2 id="17-1体验Lambda表达式"   >
          <a href="#17-1体验Lambda表达式" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-1体验Lambda表达式" class="headerlink" title="17.1体验Lambda表达式"></a>17.1体验Lambda表达式</h2>
      <p>函数式编程思想概述：</p>
<p>​	在数学中，函数就是有输入量、输出量的一套计算方案，也就是“拿数据做操作”。</p>
<p>​	面向对象思想强调“必须通过对象的形式来做事情”。</p>
<p>​	函数式思想则尽量忽略面向对象的复杂语法：“强调做什么，而不是以什么形式去做”。</p>
<p>​	而我们要学习的Lambda表达式就是函数式思想的体现。</p>
<p>代码演示：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    游泳接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Swimming</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">swim</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestSwimming</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 通过匿名内部类实现</span></span><br><span class="line">        goSwimming(<span class="keyword">new</span> <span class="title class_">Swimming</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">swim</span><span class="params">()</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;铁汁, 我们去游泳吧&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*  通过Lambda表达式实现</span></span><br><span class="line"><span class="comment">            理解: 对于Lambda表达式, 对匿名内部类进行了优化</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        goSwimming(() -&gt; System.out.println(<span class="string">&quot;铁汁, 我们去游泳吧&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用接口的方法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">goSwimming</span><span class="params">(Swimming swimming)</span> &#123;</span><br><span class="line">        swimming.swim();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="17-2-标准格式"   >
          <a href="#17-2-标准格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-2-标准格式" class="headerlink" title="17.2 标准格式"></a>17.2 标准格式</h2>
      <p>​	(形式参数) -&gt; {代码块}</p>
<ul>
<li>形式参数：如果有多个参数，参数之间用逗号隔开；如果没有参数，留空即可</li>
<li>-&gt;：由英文中画线和大于符号组成，固定写法。代表指向动作</li>
<li>代码块：是我们具体要做的事情，也就是以前我们写的方法体内容</li>
</ul>
<p>组成Lambda表达式的三要素：形式参数，箭头，代码块</p>

        <h2 id="17-3-使用前提"   >
          <a href="#17-3-使用前提" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-3-使用前提" class="headerlink" title="17.3 使用前提"></a>17.3 使用前提</h2>
      <ul>
<li>有一个接口</li>
<li>接口中有且仅有一个抽象方法</li>
</ul>

        <h2 id="17-4-省略模式"   >
          <a href="#17-4-省略模式" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-4-省略模式" class="headerlink" title="17.4 省略模式"></a>17.4 省略模式</h2>
      <p>省略的规则：</p>
<ul>
<li>参数类型可以省略。但是有多个参数的情况下，不能只省略一个</li>
<li>如果参数有且仅有一个，那么小括号可以省略</li>
<li>如果代码块的语句只有一条，可以省略大括号和分号（必须同时省略），如果有return，return也要省略掉</li>
</ul>

        <h2 id="17-5-Lambda表达式和匿名内部类的区别"   >
          <a href="#17-5-Lambda表达式和匿名内部类的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-5-Lambda表达式和匿名内部类的区别" class="headerlink" title="17.5 Lambda表达式和匿名内部类的区别"></a>17.5 Lambda表达式和匿名内部类的区别</h2>
      <ul>
<li>所需类型不同<ul>
<li>匿名内部类：可以是接口，也可以是抽象类，还可以是具体类</li>
<li>Lambda表达式：只能是接口</li>
</ul>
</li>
<li>使用限制不同<ul>
<li>如果接口中有且仅有一个抽象方法，可以使用Lambda表达式，也可以使用匿名内部类</li>
<li>如果接口中多于一个抽象方法，只能使用匿名内部类，而不能使用Lambda表达式</li>
</ul>
</li>
<li>实现原理不同<ul>
<li>匿名内部类：编译之后，产生一个单独的.class字节码文件</li>
<li>Lambda表达式：编译之后，没有一个单独的.class字节码文件。对应的字节码会在运行的时候动态生成</li>
</ul>
</li>
</ul>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/02/12/1.Java%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/">1.Java基础语法</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-12</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-02-13</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-概述"   >
          <a href="#1-概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h1>
      
        <h2 id="1-1-java语言的三个版本"   >
          <a href="#1-1-java语言的三个版本" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-1-java语言的三个版本" class="headerlink" title="1.1 java语言的三个版本"></a>1.1 java语言的三个版本</h2>
      <p> JavaSE: Java 语言的（标准版），用于桌面应用的开发，是其他两个版本的基础</p>
<p> JavaME: Java 语言的（小型版），用于嵌入式消费类电子设备</p>
<p> JavaEE: Java 语言的（企业版），用于 Web 方向的网站开发</p>

        <h2 id="1-2-Java语言跨平台原理"   >
          <a href="#1-2-Java语言跨平台原理" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-2-Java语言跨平台原理" class="headerlink" title="1.2 Java语言跨平台原理"></a>1.2 Java语言跨平台原理</h2>
      <p>Java编译器将Java源程序编译成与平台无关的字节码文件(class文件)，然后由Java虚拟机（JVM）对字节码文件解释执行。</p>
<p><img src="https://s2.loli.net/2022/02/12/7fm82MspKjnFNbA.png" alt="Java跨平台原理"></p>

        <h2 id="1-3-JRE和JDK"   >
          <a href="#1-3-JRE和JDK" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-JRE和JDK" class="headerlink" title="1.3 JRE和JDK"></a>1.3 JRE和JDK</h2>
      <p>JVM（Java Virtual Machine），Java虚拟机</p>
<p>JRE（Java Runtime Environment），Java运行环境，包含了JVM和Java的核心类库（Java API）</p>
<p>JDK（Java Development Kit）称为Java开发工具，包含了JRE和开发工具</p>
<p>总结：我们只需安装JDK即可，它包含了java的运行环境和虚拟机。</p>
<p><img src="https://s2.loli.net/2022/02/12/s4kZ126ljcCrARx.png" alt="image-20220212185728089"></p>

        <h3 id="1-3-1-JDK的安装目录介绍"   >
          <a href="#1-3-1-JDK的安装目录介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-3-1-JDK的安装目录介绍" class="headerlink" title="1.3.1 JDK的安装目录介绍"></a>1.3.1 JDK的安装目录介绍</h3>
      <div class="table-container"><table>
<thead>
<tr>
<th>目录名称</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>bin</td>
<td>该路径下存放了JDK的各种工具命令。javac和java就放在这个目录。</td>
</tr>
<tr>
<td>conf</td>
<td>该路径下存放了JDK的相关配置文件。</td>
</tr>
<tr>
<td>include</td>
<td>该路径下存放了一些平台特定的头文件。</td>
</tr>
<tr>
<td>jmods</td>
<td>该路径下存放了JDK的各种模块。</td>
</tr>
<tr>
<td>legal</td>
<td>该路径下存放了JDK各模块的授权文档。</td>
</tr>
<tr>
<td>lib</td>
<td>该路径下存放了JDK工具的一些补充JAR包。</td>
</tr>
</tbody></table></div>

        <h2 id="1-4-HelloWorld案例的编译和运行"   >
          <a href="#1-4-HelloWorld案例的编译和运行" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-4-HelloWorld案例的编译和运行" class="headerlink" title="1.4 HelloWorld案例的编译和运行"></a>1.4 HelloWorld案例的编译和运行</h2>
      <p>打开命令行窗口，将目录切换至java文件所在目录，编译java文件生成class文件，运行class文件。</p>
<blockquote>
<p>编译：javac 文件名.java</p>
<p>范例：javac HelloWorld.java</p>
<p>运行：java 类名</p>
<p>范例：java HelloWorld</p>
</blockquote>

        <h2 id="1-5-关键字"   >
          <a href="#1-5-关键字" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-5-关键字" class="headerlink" title="1.5 关键字"></a>1.5 关键字</h2>
      <p>关键字是指被java语言赋予了特殊含义的单词。</p>
<p>关键字的特点：</p>
<p>​	关键字的字母全部小写。</p>
<p>​	常用的代码编辑器对关键字都有高亮显示，比如现在我们能看到的public、class、static等。</p>

        <h2 id="1-6-常量"   >
          <a href="#1-6-常量" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-6-常量" class="headerlink" title="1.6 常量"></a>1.6 常量</h2>
      <p>常量：在程序运行过程中，其值不可以发生改变的量。</p>
<p>Java中的常量分类：</p>
<blockquote>
<p>字符串常量  用双引号括起来的多个字符（可以包含0个、一个或多个），例如”a”、”abc”、”中国”等</p>
<p>整数常量  整数，例如：-10、0、88等</p>
<p>小数常量  小数，例如：-5.5、1.0、88.88等</p>
<p>字符常量  用单引号括起来的一个字符，例如：’a’、’5’、’B’、’中’等</p>
<p>布尔常量  布尔值，表示真假，只有两个值true和false</p>
<p>空常量  一个特殊的值，空值，值为null</p>
</blockquote>
<p>除空常量外，其他常量均可使用输出语句直接输出。</p>

        <h2 id="1-7-变量"   >
          <a href="#1-7-变量" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-变量" class="headerlink" title="1.7 变量"></a>1.7 变量</h2>
      
        <h3 id="1-7-1-变量的定义"   >
          <a href="#1-7-1-变量的定义" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-1-变量的定义" class="headerlink" title="1.7.1 变量的定义"></a>1.7.1 变量的定义</h3>
      <p>变量：在程序运行过程中，其值可以发生改变的量。</p>
<p>从本质上讲，变量是内存中的一小块区域，其值可以在一定范围内变化。</p>
<p>变量的定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">数据类型 变量名 = 初始化值; <span class="comment">// 声明变量并赋值</span></span><br><span class="line"><span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">18</span>;</span><br><span class="line">System.out.println(age);</span><br></pre></td></tr></table></div></figure>

<p>或者(扩展)</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 先声明，后赋值（使用前赋值即可）</span></span><br><span class="line">数据类型 变量名;</span><br><span class="line">变量名 = 初始化值;</span><br><span class="line"><span class="type">double</span> money;</span><br><span class="line">money = <span class="number">55.5</span>;</span><br><span class="line">System.out.println(money);</span><br></pre></td></tr></table></div></figure>

<p>还可以(扩展)</p>
<p>在同一行定义多个同一种数据类型的变量，中间使用逗号隔开。但不建议使用这种方式，降低程序的可读性。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>, b = <span class="number">20</span>; <span class="comment">// 定义int类型的变量a和b，中间使用逗号隔开</span></span><br><span class="line">System.out.println(a);</span><br><span class="line">System.out.println(b);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> c,d; <span class="comment">// 声明int类型的变量c和d，中间使用逗号隔开</span></span><br><span class="line">c = <span class="number">30</span>;</span><br><span class="line">d = <span class="number">40</span>;</span><br><span class="line">System.out.println(c);</span><br><span class="line">System.out.println(d);</span><br></pre></td></tr></table></div></figure>


        <h3 id="1-7-2-变量的修改"   >
          <a href="#1-7-2-变量的修改" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-2-变量的修改" class="headerlink" title="1.7.2 变量的修改"></a>1.7.2 变量的修改</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">a = <span class="number">30</span>;  <span class="comment">//修改变量的值</span></span><br><span class="line">System.out.println(a);</span><br></pre></td></tr></table></div></figure>

<p>变量前面不加数据类型时，表示修改已存在的变量的值。</p>

        <h3 id="1-7-3-变量的注意事项"   >
          <a href="#1-7-3-变量的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-7-3-变量的注意事项" class="headerlink" title="1.7.3 变量的注意事项"></a>1.7.3 变量的注意事项</h3>
      <ol>
<li>在同一对花括号中，变量名不能重复。</li>
<li>变量在使用之前，必须初始化（赋值）。</li>
<li>定义long类型的变量时，需要在整数的后面加L（大小写均可，建议大写）。因为整数默认是int类型，整数太大可能超出int范围。</li>
<li>定义float类型的变量时，需要在小数的后面加F（大小写均可，建议大写）。因为浮点数的默认类型是double， double的取值范围是大于float的，类型不兼容。</li>
</ol>

        <h2 id="1-8-数据类型"   >
          <a href="#1-8-数据类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-8-数据类型" class="headerlink" title="1.8 数据类型"></a>1.8 数据类型</h2>
      <p>Java是一个强类型语言，Java中的数据必须明确数据类型。在Java中的数据类型包括基本数据类型和引用数据类型两种。</p>
<p>Java中的基本数据类型：</p>
<div class="table-container"><table>
<thead>
<tr>
<th align="left">数据类型</th>
<th>关键字</th>
<th>内存占用</th>
<th align="left">取值范围</th>
</tr>
</thead>
<tbody><tr>
<td align="left">整数类型</td>
<td>byte</td>
<td>1</td>
<td align="left">-128~127</td>
</tr>
<tr>
<td align="left"></td>
<td>short</td>
<td>2</td>
<td align="left">-32768~32767</td>
</tr>
<tr>
<td align="left"></td>
<td>int(默认)</td>
<td>4</td>
<td align="left">-2的31次方到2的31次方-1</td>
</tr>
<tr>
<td align="left"></td>
<td>long</td>
<td>8</td>
<td align="left">-2的63次方到2的63次方-1</td>
</tr>
<tr>
<td align="left">浮点类型</td>
<td>float</td>
<td>4</td>
<td align="left">负数：-3.402823E+38到-1.401298E-45                                                             正数：   1.401298E-45到3.402823E+38</td>
</tr>
<tr>
<td align="left"></td>
<td>double(默认)</td>
<td>8</td>
<td align="left">负数：-1.797693E+308到-4.9000000E-324                                              正数：4.9000000E-324   到1.797693E+308</td>
</tr>
<tr>
<td align="left">字符类型</td>
<td>char</td>
<td>2</td>
<td align="left">0-65535</td>
</tr>
<tr>
<td align="left">布尔类型</td>
<td>boolean</td>
<td>1</td>
<td align="left">true，false</td>
</tr>
</tbody></table></div>
<p>说明：</p>
<p>​	e+38表示是乘以10的38次方，同样，e-45表示乘以10的负45次方。</p>
<p>​	在java中整数默认是int类型，浮点数默认是double类型。</p>

        <h2 id="1-9-标识符"   >
          <a href="#1-9-标识符" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-标识符" class="headerlink" title="1.9 标识符"></a>1.9 标识符</h2>
      <p>标识符是用户编程时使用的名字，用于给类、方法、变量、常量等命名。</p>

        <h3 id="1-9-1-组成规则："   >
          <a href="#1-9-1-组成规则：" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-1-组成规则：" class="headerlink" title="1.9.1 组成规则："></a>1.9.1 组成规则：</h3>
      <p>​	由字母、数字、下划线“_”、美元符号“$”组成，第一个字符不能是数字。</p>
<p>​	不能使用java中的关键字作为标识符。	</p>
<p>​	标识符对大小写敏感（区分大小写）。</p>

        <h3 id="1-9-2-命名约定："   >
          <a href="#1-9-2-命名约定：" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-命名约定：" class="headerlink" title="1.9.2 命名约定："></a>1.9.2 命名约定：</h3>
      
        <h4 id="1-9-2-1-小驼峰式命名：变量名、方法名"   >
          <a href="#1-9-2-1-小驼峰式命名：变量名、方法名" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-1-小驼峰式命名：变量名、方法名" class="headerlink" title="1.9.2.1 小驼峰式命名：变量名、方法名"></a>1.9.2.1 小驼峰式命名：变量名、方法名</h4>
      <p>​		首字母小写，从第二个单词开始每个单词的首字母大写。</p>
<p>​		例如：userName</p>

        <h4 id="1-9-2-2-大驼峰式命名：类名"   >
          <a href="#1-9-2-2-大驼峰式命名：类名" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-9-2-2-大驼峰式命名：类名" class="headerlink" title="1.9.2.2 大驼峰式命名：类名"></a>1.9.2.2 大驼峰式命名：类名</h4>
      <p>​		每个单词的首字母都大写。</p>
<p>​		例如：Student</p>

        <h1 id="2-类型转换"   >
          <a href="#2-类型转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-类型转换" class="headerlink" title="2.类型转换"></a>2.类型转换</h1>
      <p>在Java中，一些数据类型之间是可以相互转换的。分为两种情况：自动类型转换和强制类型转换。</p>

        <h2 id="2-1-隐式转换（自动类型转换）"   >
          <a href="#2-1-隐式转换（自动类型转换）" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-1-隐式转换（自动类型转换）" class="headerlink" title="2.1 隐式转换（自动类型转换）"></a>2.1 隐式转换（自动类型转换）</h2>
      <p>​	把一个表示数据范围小的数值或者变量赋值给另一个表示数据范围大的变量。这种转换方式是自动的，直接书写即可。例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">double</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>; <span class="comment">// 将int类型的10直接赋值给double类型</span></span><br><span class="line">System.out.println(num); <span class="comment">// 输出10.0</span></span><br></pre></td></tr></table></div></figure>

<p>​	类型从小到大关系图：</p>
<p><img src="https://s2.loli.net/2022/02/12/neH3aLdwqZEvtWT.png" alt="图片1"></p>
<p>说明：</p>
<ol>
<li>整数默认是int类型，byte、short和char类型数据参与运算均会自动转换为int类型。</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">b1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b2</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> b1 + b2; </span><br><span class="line"><span class="comment">// 第三行代码会报错，b1和b2会自动转换为int类型，计算结果为int，int赋值给byte需要强制类型转换。</span></span><br><span class="line"><span class="comment">// 修改为:</span></span><br><span class="line"><span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> b1 + b2;</span><br><span class="line"><span class="comment">// 或者：</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> (<span class="type">byte</span>) (b1 + b2);</span><br></pre></td></tr></table></div></figure>

<ol start="2">
<li>boolean类型不能与其他基本数据类型相互转换。</li>
</ol>

        <h2 id="2-2-强制转换"   >
          <a href="#2-2-强制转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-2-强制转换" class="headerlink" title="2.2 强制转换"></a>2.2 强制转换</h2>
      <p>​	把一个表示数据范围大的数值或者变量赋值给另一个表示数据范围小的变量。</p>
<p>​	强制类型转换格式：目标数据类型 变量名 &#x3D; (目标数据类型)值或者变量;</p>
<p>​	例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">double</span> <span class="variable">num1</span> <span class="operator">=</span> <span class="number">5.5</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">num2</span> <span class="operator">=</span> (<span class="type">int</span>) num1; <span class="comment">// 将double类型的num1强制转换为int类型</span></span><br><span class="line">System.out.println(num2); <span class="comment">// 输出5（小数位直接舍弃）</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="2-3-类型转换案例"   >
          <a href="#2-3-类型转换案例" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-3-类型转换案例" class="headerlink" title="2.3 类型转换案例"></a>2.3 类型转换案例</h2>
      <p>案例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">c</span> <span class="operator">=</span> a + b; <span class="comment">//错误。因为两个byte变量相加，会先提升为int类型</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">d</span> <span class="operator">=</span> <span class="number">3</span> + <span class="number">4</span>; <span class="comment">//正确。常量优化机制</span></span><br></pre></td></tr></table></div></figure>

<p>常量优化机制：</p>
<p>​	在编译时，整数常量的计算会直接算出结果，并且会自动判断该结果是否在byte取值范围内，</p>
<blockquote>
<p>在：编译通过<br>        不在：编译失败</p>
</blockquote>

        <h1 id="3-运算符"   >
          <a href="#3-运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-运算符" class="headerlink" title="3.运算符"></a>3.运算符</h1>
      
        <h2 id="3-1-算数运算符"   >
          <a href="#3-1-算数运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-算数运算符" class="headerlink" title="3.1 算数运算符"></a>3.1 算数运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>+</td>
<td>加</td>
<td>和值</td>
</tr>
<tr>
<td>-</td>
<td>减</td>
<td>差值</td>
</tr>
<tr>
<td>*</td>
<td>乘</td>
<td>与“×”相同</td>
</tr>
<tr>
<td>&#x2F;</td>
<td>除</td>
<td>与“÷”相同</td>
</tr>
<tr>
<td>%</td>
<td>取余</td>
<td>获取的是两个数据做除法的余数</td>
</tr>
</tbody></table></div>
<p>注意：</p>
<ol>
<li><p>&#x2F;和%的区别：两个数据做除法，&#x2F;取结果的商，%取结果的余数。</p>
</li>
<li><p>整数操作只能得到整数，要想得到小数，必须有浮点数参与运算。</p>
</li>
</ol>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line">System.out.println(a / b); <span class="comment">// 输出结果3</span></span><br><span class="line">System.out.println(a % b); <span class="comment">// 输出结果1</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-1-字符的“-”操作"   >
          <a href="#3-1-1-字符的“-”操作" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-1-字符的“-”操作" class="headerlink" title="3.1.1 字符的“+”操作"></a>3.1.1 字符的“+”操作</h3>
      <p>char类型参与算术运算，使用的是计算机底层对应的十进制数值。需要我们记住三个字符对应的数值：</p>
<blockquote>
<p>‘a’  –  97		a-z是连续的，所以’b’对应的数值是98，’c’是99，依次递加</p>
<p>‘A’  –  65		A-Z是连续的，所以’B’对应的数值是66，’C’是67，依次递加</p>
<p>‘0’  –  48		0-9是连续的，所以’1’对应的数值是49，’2’是50，依次递加</p>
</blockquote>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可以通过使用字符与整数做算术运算，得出字符对应的数值是多少</span></span><br><span class="line"><span class="type">char</span> <span class="variable">ch1</span> <span class="operator">=</span> <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">System.out.println(ch1 + <span class="number">1</span>); <span class="comment">// 输出98，97 + 1 = 98</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> <span class="variable">ch2</span> <span class="operator">=</span> <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">System.out.println(ch2 + <span class="number">1</span>); <span class="comment">// 输出66，65 + 1 = 66</span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> <span class="variable">ch3</span> <span class="operator">=</span> <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">System.out.println(ch3 + <span class="number">1</span>); <span class="comment">// 输出49，48 + 1 = 49</span></span><br></pre></td></tr></table></div></figure>

<p>算术表达式中包含不同的基本数据类型的值的时候，整个算术表达式的类型会自动进行提升。</p>
<p>提升规则：</p>
<p>byte类型，short类型和char类型将被提升到int类型，不管是否有其他类型参与运算。</p>
<p>整个表达式的类型自动提升到与表达式中最高等级的操作数相同的类型</p>
<p>​       等级顺序：byte,short,char –&gt; int –&gt; long –&gt; float –&gt; double</p>
<p>例如：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">byte</span> <span class="variable">b1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">byte</span> <span class="variable">b2</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="comment">// byte b3 = b1 + b2; // 该行报错，因为byte类型参与算术运算会自动提示为int，int赋值给byte可能损失精度</span></span><br><span class="line"><span class="type">int</span> <span class="variable">i3</span> <span class="operator">=</span> b1 + b2; <span class="comment">// 应该使用int接收</span></span><br><span class="line"><span class="type">byte</span> <span class="variable">b3</span> <span class="operator">=</span> (<span class="type">byte</span>) (b1 + b2); <span class="comment">// 或者将结果强制转换为byte类型</span></span><br><span class="line">-------------------------------</span><br><span class="line"><span class="type">int</span> <span class="variable">num1</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">double</span> <span class="variable">num2</span> <span class="operator">=</span> <span class="number">20.0</span>;</span><br><span class="line"><span class="type">double</span> <span class="variable">num3</span> <span class="operator">=</span> num1 + num2; <span class="comment">// 使用double接收，因为num1会自动提升为double类型</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="3-1-2-字符串的“-”操作"   >
          <a href="#3-1-2-字符串的“-”操作" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-1-2-字符串的“-”操作" class="headerlink" title="3.1.2 字符串的“+”操作"></a>3.1.2 字符串的“+”操作</h3>
      <p>当“+”操作中出现字符串时，这个”+”是字符串连接符，而不是算术运算。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="string">&quot;itheima&quot;</span>+ <span class="number">666</span>); <span class="comment">// 输出：itheima666</span></span><br></pre></td></tr></table></div></figure>

<p>在”+”操作中，如果出现了字符串，就是连接运算符，否则就是算术运算。当连续进行“+”操作时，从左到右逐个执行。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">System.out.println(<span class="number">1</span> + <span class="number">99</span> + <span class="string">&quot;年黑马&quot;</span>);            <span class="comment">// 输出：100年黑马</span></span><br><span class="line">System.out.println(<span class="number">1</span> + <span class="number">2</span> + <span class="string">&quot;itheima&quot;</span> + <span class="number">3</span> + <span class="number">4</span>);   <span class="comment">// 输出：3itheima34</span></span><br><span class="line"><span class="comment">// 可以使用小括号改变运算的优先级 </span></span><br><span class="line">System.out.println(<span class="number">1</span> + <span class="number">2</span> + <span class="string">&quot;itheima&quot;</span> + (<span class="number">3</span> + <span class="number">4</span>)); <span class="comment">// 输出：3itheima7</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-2-自增自减运算符"   >
          <a href="#3-2-自增自减运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-2-自增自减运算符" class="headerlink" title="3.2 自增自减运算符"></a>3.2 自增自减运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>++</td>
<td>自增</td>
<td>变量的值加1</td>
</tr>
<tr>
<td>–</td>
<td>自减</td>
<td>变量的值减1</td>
</tr>
</tbody></table></div>
<p>注意事项：</p>
<p>​	++和– 既可以放在变量的后边，也可以放在变量的前边。</p>
<p>​	单独使用的时候， ++和– 无论是放在变量的前边还是后边，结果是一样的。</p>
<p>​	放在后面：先运算，后自加（减）。</p>
<p>​	放在前面：先自加（减），后运算。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">i++; <span class="comment">// 单独使用</span></span><br><span class="line">System.out.println(<span class="string">&quot;i:&quot;</span> + i); <span class="comment">// i:11</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">++j; <span class="comment">// 单独使用</span></span><br><span class="line">System.out.println(<span class="string">&quot;j:&quot;</span> + j); <span class="comment">// j:11</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> x++; <span class="comment">// 赋值运算，++在后边，所以是使用x原来的值赋值给y，x本身自增1</span></span><br><span class="line">System.out.println(<span class="string">&quot;x:&quot;</span> + x + <span class="string">&quot;, y:&quot;</span> + y); <span class="comment">// x:11，y:10</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> ++m; <span class="comment">// 赋值运算，++在前边，所以是使用m自增后的值赋值给n，m本身自增1</span></span><br><span class="line">System.out.println(<span class="string">&quot;m:&quot;</span> + m + <span class="string">&quot;, m:&quot;</span> + m); <span class="comment">// m:11，m:11</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-3-赋值运算符"   >
          <a href="#3-3-赋值运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-3-赋值运算符" class="headerlink" title="3.3 赋值运算符"></a>3.3 赋值运算符</h2>
      <p>赋值运算符的作用是将一个表达式的值赋给左边，左边必须是可修改的，不能是常量。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&#x3D;</td>
<td>赋值</td>
<td>a&#x3D;10，将10赋值给变量a</td>
</tr>
<tr>
<td>+&#x3D;</td>
<td>加后赋值</td>
<td>a+&#x3D;b，将a+b的值给a</td>
</tr>
<tr>
<td>-&#x3D;</td>
<td>减后赋值</td>
<td>a-&#x3D;b，将a-b的值给a</td>
</tr>
<tr>
<td>*&#x3D;</td>
<td>乘后赋值</td>
<td>a*&#x3D;b，将a×b的值给a</td>
</tr>
<tr>
<td>&#x2F;&#x3D;</td>
<td>除后赋值</td>
<td>a&#x2F;&#x3D;b，将a÷b的商给a</td>
</tr>
<tr>
<td>%&#x3D;</td>
<td>取余后赋值</td>
<td>a%&#x3D;b，将a÷b的余数给a</td>
</tr>
</tbody></table></div>
<p>注意：</p>
<p>扩展的赋值运算符&#x3D;&#x3D;隐含了强制类型转换&#x3D;&#x3D;。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">short</span> <span class="variable">s</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">s = s + <span class="number">10</span>; <span class="comment">// 此行代码报出，因为运算中s提升为int类型，运算结果int赋值给short可能损失精度</span></span><br><span class="line"></span><br><span class="line">s += <span class="number">10</span>; <span class="comment">// 此行代码没有问题，隐含了强制类型转换，相当于 s = (short) (s + 10);</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-4-关系运算符"   >
          <a href="#3-4-关系运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-4-关系运算符" class="headerlink" title="3.4 关系运算符"></a>3.4 关系运算符</h2>
      <p>关系运算符有6种关系，分别为小于、小于等于、大于、等于、大于等于、不等于。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&#x3D;&#x3D;</td>
<td>a&#x3D;&#x3D;b，判断a和b的值是否相等，成立为true，不成立为false</td>
</tr>
<tr>
<td>!&#x3D;</td>
<td>a!&#x3D;b，判断a和b的值是否不相等，成立为true，不成立为false</td>
</tr>
<tr>
<td>&gt;</td>
<td>a&gt;b，判断a是否大于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&gt;&#x3D;</td>
<td>a&gt;&#x3D;b，判断a是否大于等于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&lt;</td>
<td>a&lt;b，判断a是否小于b，成立为true，不成立为false</td>
</tr>
<tr>
<td>&lt;&#x3D;</td>
<td>a&lt;&#x3D;b，判断a是否小于等于b，成立为true，不成立为false</td>
</tr>
</tbody></table></div>

        <h2 id="3-5-逻辑运算符"   >
          <a href="#3-5-逻辑运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-5-逻辑运算符" class="headerlink" title="3.5 逻辑运算符"></a>3.5 逻辑运算符</h2>
      <p>逻辑运算符把各个运算的关系表达式连接起来组成一个复杂的逻辑表达式，以判断程序中的表达式是否成立，判断的结果是 true 或 false。</p>
<div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;</td>
<td>逻辑与</td>
<td>a&amp;b，a和b都是true，结果为true，否则为false</td>
</tr>
<tr>
<td>|</td>
<td>逻辑或</td>
<td>a|b，a和b都是false，结果为false，否则为true</td>
</tr>
<tr>
<td>^</td>
<td>逻辑异或</td>
<td>a^b，a和b结果不同为true，相同为false</td>
</tr>
<tr>
<td>!</td>
<td>逻辑非</td>
<td>!a，结果和a的结果正好相反</td>
</tr>
</tbody></table></div>

        <h2 id="3-6-短路逻辑运算符"   >
          <a href="#3-6-短路逻辑运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-6-短路逻辑运算符" class="headerlink" title="3.6 短路逻辑运算符"></a>3.6 短路逻辑运算符</h2>
      <div class="table-container"><table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>&amp;&amp;</td>
<td>短路与</td>
<td>作用和&amp;相同，但是有短路效果</td>
</tr>
<tr>
<td>||</td>
<td>短路或</td>
<td>作用和|相同，但是有短路效果</td>
</tr>
</tbody></table></div>
<p>在逻辑与运算中，只要有一个表达式的值为false，那么结果就可以判定为false了，没有必要将所有表达式的值都计算出来，短路与操作就有这样的效果，可以提高效率。同理在逻辑或运算中，一旦发现值为true，右边的表达式将不再参与运算。</p>
<ul>
<li><p>逻辑与&amp;，无论左边真假，右边都要执行。</p>
</li>
<li><p>短路与&amp;&amp;，如果左边为真，右边执行；如果左边为假，右边不执行。</p>
</li>
<li><p>逻辑或|，无论左边真假，右边都要执行。</p>
</li>
<li><p>短路或||，如果左边为假，右边执行；如果左边为真，右边不执行。</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line">System.out.println((x++ &gt; <span class="number">4</span>) &amp; (y++ &gt; <span class="number">5</span>)); <span class="comment">// 两个表达都会运算</span></span><br><span class="line">System.out.println(x); <span class="comment">// 4</span></span><br><span class="line">System.out.println(y); <span class="comment">// 5</span></span><br><span class="line"></span><br><span class="line">System.out.println((x++ &gt; <span class="number">4</span>) &amp;&amp; (y++ &gt; <span class="number">5</span>)); <span class="comment">// 左边已经可以确定结果为false，右边不参与运算</span></span><br><span class="line">System.out.println(x); <span class="comment">// 4</span></span><br><span class="line">System.out.println(y); <span class="comment">// 4</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="3-7-三元运算符"   >
          <a href="#3-7-三元运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-7-三元运算符" class="headerlink" title="3.7 三元运算符"></a>3.7 三元运算符</h2>
      <p>三元运算符语法格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">关系表达式 ? 表达式<span class="number">1</span> : 表达式<span class="number">2</span>;</span><br></pre></td></tr></table></div></figure>

<p>解释：问号前面的位置是判断的条件，判断结果为boolean型，为true时调用表达式1，为false时调用表达式2。其逻辑为：如果条件表达式成立或者满足则执行表达式1，否则执行第二个。</p>
<p>举例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line"><span class="type">int</span> <span class="variable">c</span> <span class="operator">=</span> a &gt; b ? a : b; <span class="comment">// 判断 a&gt;b 是否为真，如果为真取a的值，如果为假，取b的值</span></span><br></pre></td></tr></table></div></figure>




        <h1 id="4-流程控制语句"   >
          <a href="#4-流程控制语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-流程控制语句" class="headerlink" title="4. 流程控制语句"></a>4. 流程控制语句</h1>
      
        <h2 id="4-1-顺序结构"   >
          <a href="#4-1-顺序结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-1-顺序结构" class="headerlink" title="4.1 顺序结构"></a>4.1 顺序结构</h2>
      <p>顺序结构是程序中最简单最基本的流程控制，没有特定的语法结构，按照代码的先后顺序，依次执行，程序中大多数的代码都是这样执行的。</p>
<p>顺序结构执行流程图：</p>
<p><img src="https://s2.loli.net/2022/02/12/BrsQhcxOFLZ1Kp8.png" alt="image-20220212205710165"></p>

        <h2 id="4-2-分支结构"   >
          <a href="#4-2-分支结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-分支结构" class="headerlink" title="4.2 分支结构"></a>4.2 分支结构</h2>
      
        <h3 id="4-2-1-if语句格式1"   >
          <a href="#4-2-1-if语句格式1" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-1-if语句格式1" class="headerlink" title="4.2.1  if语句格式1"></a>4.2.1  if语句格式1</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式) &#123;</span><br><span class="line">    语句体;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式的值</p>
<p>②如果关系表达式的值为true就执行语句体</p>
<p>③如果关系表达式的值为false就不执行语句体</p>
<p>④继续执行后面的语句内容</p>
<p><img src="https://s2.loli.net/2022/02/12/e2UGxc6ZXRSvphk.png" alt="image-20220212205853564"></p>

        <h3 id="4-2-2-if语句格式2"   >
          <a href="#4-2-2-if语句格式2" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-2-if语句格式2" class="headerlink" title="4.2.2 if语句格式2"></a>4.2.2 if语句格式2</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式) &#123;</span><br><span class="line">    语句体<span class="number">1</span>;	</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    语句体<span class="number">2</span>;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式的值</p>
<p>②如果关系表达式的值为true就执行语句体1</p>
<p>③如果关系表达式的值为false就执行语句体2</p>
<p>④继续执行后面的语句内容</p>
<p><img src="https://s2.loli.net/2022/02/12/3wyDlNK9GsEjoVx.png" alt="image-20220212210008323"></p>

        <h3 id="4-2-3-if语句格式3"   >
          <a href="#4-2-3-if语句格式3" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-3-if语句格式3" class="headerlink" title="4.2.3 if语句格式3"></a>4.2.3 if语句格式3</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">格式：</span><br><span class="line"><span class="keyword">if</span> (关系表达式<span class="number">1</span>) &#123;</span><br><span class="line">    语句体<span class="number">1</span>;	</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (关系表达式<span class="number">2</span>) &#123;</span><br><span class="line">    语句体<span class="number">2</span>;	</span><br><span class="line">&#125; </span><br><span class="line">…</span><br><span class="line"><span class="keyword">else</span> &#123;</span><br><span class="line">    语句体n+<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>执行流程：</p>
<p>①首先计算关系表达式1的值</p>
<p>②如果值为true就执行语句体1；如果值为false就计算关系表达式2的值</p>
<p>③如果值为true就执行语句体2；如果值为false就计算关系表达式3的值</p>
<p>④…</p>
<p>⑤如果没有任何关系表达式为true，就执行语句体n+1。</p>
<p><img src="https://s2.loli.net/2022/02/12/nQDogGRCWe7Tqbv.png" alt="图片5"></p>

        <h2 id="4-2-循环结构"   >
          <a href="#4-2-循环结构" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-循环结构" class="headerlink" title="4.2 循环结构"></a>4.2 循环结构</h2>
      
        <h3 id="4-2-1-switch语句"   >
          <a href="#4-2-1-switch语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-1-switch语句" class="headerlink" title="4.2.1 switch语句"></a>4.2.1 switch语句</h3>
      <ul>
<li><p>格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (表达式) &#123;</span><br><span class="line">	<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">		语句体<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">		语句体<span class="number">2</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	...</span><br><span class="line">	<span class="keyword">default</span>:</span><br><span class="line">		语句体n+<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>执行流程：</p>
<ul>
<li>首先计算出表达式的值 </li>
<li>其次，和case依次比较，一旦有对应的值，就会执行相应的语句，在执行的过程中，遇到break就会结束。 </li>
<li>最后，如果所有的case都和表达式的值不匹配，就会执行default语句体部分，然后程序结束掉。</li>
</ul>
</li>
</ul>
<p>注意： 如果switch语句中,case省略了break语句, 就会开始case穿透</p>
<ul>
<li>需求 : 键盘录入星期数，输出工作日、休息日 (1-5)工作日，(6-7)休息日</li>
<li>示例代码：</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">case穿透是如何产生的?</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		如果switch语句中,case省略了break语句, 就会开始case穿透.</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		现象：</span></span><br><span class="line"><span class="comment">			当开始case穿透，后续的case就不会具有匹配效果，内部的语句都会执行</span></span><br><span class="line"><span class="comment">			直到看见break，或者将整体switch语句执行完毕，才会结束。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">		<span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">		System.out.println(<span class="string">&quot;请输入星期数:&quot;</span>);</span><br><span class="line">		<span class="type">int</span> <span class="variable">week</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">switch</span>(week)&#123;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;工作日&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">			<span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;休息日&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">default</span>:</span><br><span class="line">				System.out.println(<span class="string">&quot;您的输入有误&quot;</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="4-2-2-for循环"   >
          <a href="#4-2-2-for循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-2-for循环" class="headerlink" title="4.2.2 for循环"></a>4.2.2 for循环</h3>
      <ul>
<li><p>循环：</p>
<p>循环语句可以在满足循环条件的情况下，反复执行某一段代码，这段被重复执行的代码被称为循环体语句，当反复 执行这个循环体时，需要在合适的时候把循环判断条件修改为false，从而结束循环，否则循环将一直执行下去，形 成死循环。 </p>
</li>
<li><p>for循环格式：</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (初始化语句;条件判断语句;条件控制语句) &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ul>
<li><p>格式解释：</p>
<ul>
<li>初始化语句：  用于表示循环开启时的起始状态，简单说就是循环开始的时候什么样</li>
<li>条件判断语句：用于表示循环反复执行的条件，简单说就是判断循环是否能一直执行下去</li>
<li>循环体语句：  用于表示循环反复执行的内容，简单说就是循环反复执行的事情</li>
<li>条件控制语句：用于表示循环执行中每次变化的内容，简单说就是控制循环是否能执行下去</li>
</ul>
</li>
<li><p>执行流程：</p>
<p>①执行初始化语句</p>
<p>②执行条件判断语句，看其结果是true还是false</p>
<p>​             如果是false，循环结束</p>
<p>​             如果是true，继续执行</p>
<p>③执行循环体语句</p>
<p>④执行条件控制语句</p>
<p>⑤回到②继续</p>
</li>
</ul>

        <h3 id="4-2-3-while循环"   >
          <a href="#4-2-3-while循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-3-while循环" class="headerlink" title="4.2.3 while循环"></a>4.2.3 while循环</h3>
      <ul>
<li><p>while循环完整格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">初始化语句;</span><br><span class="line"><span class="keyword">while</span> (条件判断语句) &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">    条件控制语句;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>while循环执行流程：</p>
<p>①执行初始化语句</p>
<p>②执行条件判断语句，看其结果是true还是false</p>
<p>​             如果是false，循环结束</p>
<p>​             如果是true，继续执行</p>
<p>③执行循环体语句</p>
<p>④执行条件控制语句</p>
<p>⑤回到②继续</p>
</li>
</ul>

        <h3 id="4-2-4-do……while循环"   >
          <a href="#4-2-4-do……while循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-4-do……while循环" class="headerlink" title="4.2.4 do……while循环"></a>4.2.4 do……while循环</h3>
      <ul>
<li><p>完整格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">初始化语句;</span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">	循环体语句;</span><br><span class="line">	条件控制语句;</span><br><span class="line">&#125;<span class="keyword">while</span>(条件判断语句);</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>执行流程：</p>
<p>① 执行初始化语句</p>
<p>② 执行循环体语句</p>
<p>③ 执行条件控制语句</p>
<p>④ 执行条件判断语句，看其结果是true还是false</p>
<p>如果是false，循环结束</p>
<p>如果是true，继续执行</p>
<p>⑤ 回到②继续</p>
</li>
</ul>

        <h3 id="4-2-5-三种循环的区别"   >
          <a href="#4-2-5-三种循环的区别" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-5-三种循环的区别" class="headerlink" title="4.2.5 三种循环的区别"></a>4.2.5 三种循环的区别</h3>
      <ul>
<li>三种循环的区别<ul>
<li>for循环和while循环先判断条件是否成立，然后决定是否执行循环体（先判断后执行）</li>
<li>do…while循环先执行一次循环体，然后判断条件是否成立，是否继续执行循环体（先执行后判断）</li>
</ul>
</li>
<li>for循环和while的区别<ul>
<li>条件控制语句所控制的自增变量，因为归属for循环的语法结构中，在for循环结束后，就不能再次被访问到了</li>
<li>条件控制语句所控制的自增变量，对于while循环来说不归属其语法结构中，在while循环结束后，该变量还可以继续使用</li>
</ul>
</li>
</ul>

        <h3 id="4-2-6-死循环"   >
          <a href="#4-2-6-死循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-6-死循环" class="headerlink" title="4.2.6 死循环"></a>4.2.6 死循环</h3>
      <ul>
<li><p>死循环格式		</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">for死循环格式 :</span><br><span class="line">for(;;)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">while死循环格式 :</span><br><span class="line"></span><br><span class="line">while(true)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">do..while死循环格式 :</span><br><span class="line"></span><br><span class="line">do&#123;</span><br><span class="line"></span><br><span class="line">&#125;while(true);</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="4-2-7-跳转控制语句"   >
          <a href="#4-2-7-跳转控制语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-2-7-跳转控制语句" class="headerlink" title="4.2.7 跳转控制语句"></a>4.2.7 跳转控制语句</h3>
      <ul>
<li>跳转控制语句（break）<ul>
<li>跳出循环，结束循环</li>
</ul>
</li>
<li>跳转控制语句（continue）<ul>
<li>跳过本次循环，继续下次循环</li>
</ul>
</li>
<li>注意： continue只能在循环中进行使用！</li>
</ul>

        <h1 id="5-Random"   >
          <a href="#5-Random" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-Random" class="headerlink" title="5.Random"></a>5.Random</h1>
      
        <h2 id="5-1-Random产生随机数（掌握）"   >
          <a href="#5-1-Random产生随机数（掌握）" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-1-Random产生随机数（掌握）" class="headerlink" title="5.1 Random产生随机数（掌握）"></a>5.1 Random产生随机数（掌握）</h2>
      <ul>
<li><p>概述：</p>
<ul>
<li>Random类似Scanner，也是Java提供好的API，内部提供了产生随机数的功能</li>
</ul>
</li>
<li><p>使用步骤：</p>
<ol>
<li><p>导入包</p>
<p><code>import java.util.Random;</code></p>
</li>
<li><p>创建对象</p>
<p><code>Random r = new Random();</code></p>
</li>
<li><p>产生随机数</p>
<p><code>int num = r.nextInt(10);</code></p>
<p>解释：产生的随机数是0-9中随机的一个</p>
</li>
</ol>
</li>
<li><p>示例代码：</p>
</li>
</ul>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Random</span> &#123;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">		Random : 产生随机数</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		1. 导包	: import java.util.Random;</span></span><br><span class="line"><span class="comment">				    导包的动作必须出现在类定义的上面</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		2. 创建对象 : Random r = new Random();</span></span><br><span class="line"><span class="comment">					上面这个格式里面，r 是变量名，可以变，其他的都不允许变</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		3. 获取随机数 : int number = r.nextInt(10);	//获取数据的范围：[0,10) 包括0,不包括10</span></span><br><span class="line"><span class="comment">					上面这个格式里面，number是变量名，可以变，数字10可以变。其他的都不允许变</span></span><br><span class="line"><span class="comment">		</span></span><br><span class="line"><span class="comment">		需求: 产生随机数1-10之间的</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">		<span class="comment">// 2. 创建对象</span></span><br><span class="line">		<span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">10</span>; i++)&#123;</span><br><span class="line">			<span class="comment">// 3. 获取随机数</span></span><br><span class="line">			<span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> r.nextInt(<span class="number">10</span>) + <span class="number">1</span>;		<span class="comment">// 1-10</span></span><br><span class="line">			System.out.println(num);</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h1 id="6-数组"   >
          <a href="#6-数组" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-数组" class="headerlink" title="6.数组"></a>6.数组</h1>
      
        <h2 id="6-1-数组介绍"   >
          <a href="#6-1-数组介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-1-数组介绍" class="headerlink" title="6.1 数组介绍"></a>6.1 数组介绍</h2>
      <p>​	数组就是存储数据长度固定的容器，存储多个数据的数据类型要一致。 </p>

        <h2 id="6-2-数组的定义格式"   >
          <a href="#6-2-数组的定义格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-2-数组的定义格式" class="headerlink" title="6.2 数组的定义格式"></a>6.2 数组的定义格式</h2>
      <ul>
<li>第一种格式</li>
</ul>
<p>​	数据类型[] 数组名</p>
<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span>[] arr;        </span><br><span class="line"><span class="type">double</span>[] arr;      </span><br><span class="line"><span class="type">char</span>[] arr;</span><br></pre></td></tr></table></div></figure>

<ul>
<li>第二种格式</li>
</ul>
<p>​	数据类型 数组名[]</p>
<p>​	示例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr[];</span><br><span class="line"><span class="type">double</span> arr[];</span><br><span class="line"><span class="type">char</span> arr[];</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-3-动态初始化"   >
          <a href="#6-3-动态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-动态初始化" class="headerlink" title="6.3 动态初始化"></a>6.3 动态初始化</h2>
      <p>数组动态初始化就是只给定数组的长度，由系统给出默认初始化值</p>

        <h3 id="6-3-1-动态初始化格式"   >
          <a href="#6-3-1-动态初始化格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-1-动态初始化格式" class="headerlink" title="6.3.1 动态初始化格式"></a>6.3.1 动态初始化格式</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = <span class="keyword">new</span> 数据类型[数组长度];</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>];</span><br></pre></td></tr></table></div></figure>


        <h3 id="6-3-2-示例代码"   >
          <a href="#6-3-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-3-2-示例代码" class="headerlink" title="6.3.2 示例代码 :"></a>6.3.2 示例代码 :</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.array;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        数组的动态初始化:</span></span><br><span class="line"><span class="comment">                        在初始化的时候, 需要手动指定数组的长度, 系统会为数组容器分配初始值.</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        动态初始化格式:</span></span><br><span class="line"><span class="comment">                        数据类型[] 数组名 = new 数据类型[数组的长度];</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        注意:</span></span><br><span class="line"><span class="comment">                        打印数组变量的时候, 会打印出数组的内存地址</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        [I@10f87f48 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                        @ : 分隔符</span></span><br><span class="line"><span class="comment">                        [ : 当前的空间是一个数组类型</span></span><br><span class="line"><span class="comment">                        I : 当前数组容器中所存储的数据类型</span></span><br><span class="line"><span class="comment">                        10f87f48 : 十六进制内存地址</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                0 1 2 3 4 5 6 7 8 9 a b c d e f</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = new 数据类型[数组的长度];</span></span><br><span class="line">        <span class="comment">// 通过new关键字创建了一个int类型的数组容器, 该容器可以存储5个int类型的整数, 该容器被arr数组变量所记录</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">5</span>];</span><br><span class="line">        <span class="comment">// [I@10f87f48</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bArr = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">3</span>];</span><br><span class="line">        <span class="comment">// [B@b4c966a</span></span><br><span class="line">        System.out.println(bArr);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="6-4-数组的静态初始化"   >
          <a href="#6-4-数组的静态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-数组的静态初始化" class="headerlink" title="6.4 数组的静态初始化"></a>6.4 数组的静态初始化</h2>
      <p>​	在创建数组时，直接将元素确定	</p>

        <h3 id="6-4-1-静态初始化格式"   >
          <a href="#6-4-1-静态初始化格式" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-1-静态初始化格式" class="headerlink" title="6.4.1 静态初始化格式"></a>6.4.1 静态初始化格式</h3>
      <ul>
<li><p>完整版格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = <span class="keyword">new</span> 数据类型[]&#123;元素<span class="number">1</span>,元素<span class="number">2</span>,...&#125;;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>简化版格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">数据类型[] 数组名 = &#123;元素<span class="number">1</span>,元素<span class="number">2</span>,...&#125;;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="6-4-2-示例代码"   >
          <a href="#6-4-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-4-2-示例代码" class="headerlink" title="6.4.2 示例代码"></a>6.4.2 示例代码</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.array2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        数组静态初始化 : 初始化时指定每个数组元素的初始值，由系统决定数组长度</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        完整格式:</span></span><br><span class="line"><span class="comment">                    数据类型[] 数组名 = new 数据类型[]&#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line"><span class="comment">        简化格式:</span></span><br><span class="line"><span class="comment">                    数据类型[] 数组名 = &#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = new 数据类型[]&#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[]&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数据类型[] 数组名 = &#123;数据1,数据2,数据3...&#125;;</span></span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line">        System.out.println(arr2);</span><br><span class="line">        System.out.println(arr2[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr2[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr2[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="6-5-内存分配"   >
          <a href="#6-5-内存分配" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-5-内存分配" class="headerlink" title="6.5 内存分配"></a>6.5 内存分配</h2>
      <ul>
<li>一个数组内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/1HGoZYEKbOgzJic.png" alt="1591007817165"></p>
<ul>
<li>两个数组内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/8lvFMXC4YUuEIAj.png" alt="1591007925899"></p>
<ul>
<li>多个数组指向相同内存图</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/57opuGVhIjN8ZLM.png" alt="1591007957052"></p>

        <h1 id="7-方法"   >
          <a href="#7-方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-方法" class="headerlink" title="7.方法"></a>7.方法</h1>
      
        <h2 id="7-1-无参数方法定义和调用"   >
          <a href="#7-1-无参数方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-1-无参数方法定义和调用" class="headerlink" title="7.1 无参数方法定义和调用"></a>7.1 无参数方法定义和调用</h2>
      <ul>
<li><p>定义格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (   ) &#123;</span><br><span class="line">	<span class="comment">// 方法体;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method</span> <span class="params">(    )</span> &#123;</span><br><span class="line">	<span class="comment">// 方法体;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>调用格式：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">方法名();</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">method();</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<ul>
<li>方法必须先定义，后调用，否则程序将报错</li>
<li>每个方法在被调用执行的时候，都会进入栈内存，并且拥有自己独立的内存空间，方法内部代码调用完毕之后，会从栈内存中弹栈消失。</li>
</ul>
</li>
</ul>

        <h2 id="7-2-带参数方法定义和调用"   >
          <a href="#7-2-带参数方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-2-带参数方法定义和调用" class="headerlink" title="7.2 带参数方法定义和调用"></a>7.2 带参数方法定义和调用</h2>
      <ul>
<li><p>定义格式：</p>
<p>参数：由数据类型和变量名组成 -  数据类型 变量名</p>
<p>参数范例：int a</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (参数<span class="number">1</span>) &#123;</span><br><span class="line">	方法体;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> 方法名 (参数<span class="number">1</span>, 参数<span class="number">2</span>, 参数<span class="number">3.</span>..) &#123;</span><br><span class="line">	方法体;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">isEvenNumber</span><span class="params">(<span class="type">int</span> number)</span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getMax</span><span class="params">(<span class="type">int</span> num1, <span class="type">int</span> num2)</span>&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">方法定义时，参数中的数据类型与变量名都不能缺少，缺少任意一个程序将报错</span><br><span class="line"></span><br><span class="line">方法定义时，多个参数之间使用逗号( ，)分隔</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>调用格式：</p>
<figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">方法名(参数)；</span><br><span class="line"></span><br><span class="line">方法名(参数1,参数2);</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">isEvenNumber(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">getMax(<span class="number">10</span>,<span class="number">20</span>);</span><br></pre></td></tr></table></div></figure>

<p>方法调用时，参数的数量与类型必须与方法定义中的设置相匹配，否则程序将报错</p>
</li>
</ul>
<p><strong>形参：方法定义中的参数</strong></p>
<p>​          等同于变量定义格式，例如：int number</p>
<p><strong>实参：方法调用中的参数</strong></p>
<p>​          等同于使用变量或常量，例如： 10  number</p>

        <h2 id="7-3-带返回值方法定义和调用"   >
          <a href="#7-3-带返回值方法定义和调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-3-带返回值方法定义和调用" class="headerlink" title="7.3 带返回值方法定义和调用"></a>7.3 带返回值方法定义和调用</h2>
      <ul>
<li><p>定义格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> 数据类型 方法名 ( 参数 ) &#123; </span><br><span class="line">	<span class="keyword">return</span> 数据 ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">boolean</span> <span class="title function_">isEvenNumber</span><span class="params">( <span class="type">int</span> number )</span> &#123;           </span><br><span class="line">	<span class="keyword">return</span> <span class="literal">true</span> ;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">getMax</span><span class="params">( <span class="type">int</span> a, <span class="type">int</span> b )</span> &#123;</span><br><span class="line">	<span class="keyword">return</span>  <span class="number">100</span> ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>注意：</p>
<p>​	方法定义时return后面的返回值与方法定义上的数据类型要匹配，否则程序将报错</p>
</li>
<li><p>调用格式</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">方法名 ( 参数 ) ;</span><br><span class="line">数据类型 变量名 = 方法名 ( 参数 ) ;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>范例</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">isEvenNumber ( <span class="number">5</span> ) ;</span><br><span class="line"><span class="type">boolean</span>  <span class="variable">flag</span> <span class="operator">=</span>  isEvenNumber ( <span class="number">5</span> ); </span><br></pre></td></tr></table></div></figure></li>
</ul>
<p>方法的返回值通常会使用变量接收，否则该返回值将无意义</p>

        <h2 id="7-4-方法的注意事项"   >
          <a href="#7-4-方法的注意事项" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-4-方法的注意事项" class="headerlink" title="7.4 方法的注意事项"></a>7.4 方法的注意事项</h2>
      <ul>
<li><p>方法不能嵌套定义</p>
<ul>
<li><p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodOne</span><span class="params">()</span> &#123;</span><br><span class="line">		<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodTwo</span><span class="params">()</span> &#123;</span><br><span class="line">       		<span class="comment">// 这里会引发编译错误!!!</span></span><br><span class="line">    	&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
<li><p>void表示无返回值，可以省略return，也可以单独的书写return，后面不加数据</p>
<ul>
<li><p>示例代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">methodTwo</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//return 100; 编译错误，因为没有具体返回值类型</span></span><br><span class="line">        <span class="keyword">return</span>;	</span><br><span class="line">        <span class="comment">//System.out.println(100); return语句后面不能跟数据或代码</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>
</li>
</ul>

        <h2 id="7-5-方法重载-同方法，不同参"   >
          <a href="#7-5-方法重载-同方法，不同参" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-5-方法重载-同方法，不同参" class="headerlink" title="7.5 方法重载(同方法，不同参)"></a>7.5 方法重载(同方法，不同参)</h2>
      <ul>
<li><p>方法重载概念</p>
<p>方法重载指同一个类中定义的多个方法之间的关系，满足下列条件的多个方法相互构成重载</p>
<ul>
<li>多个方法在同一个类中</li>
<li>多个方法具有&#x3D;&#x3D;相同的方法名&#x3D;&#x3D;</li>
<li>多个方法的&#x3D;&#x3D;参数不相同&#x3D;&#x3D;，类型不同或者数量不同</li>
</ul>
</li>
<li><p>注意：</p>
<ul>
<li>重载仅对应方法的定义，与方法的调用无关，调用方式参照标准格式</li>
<li>重载仅针对同一个类中方法的名称与参数进行识别，与返回值无关，换句话说不能通过返回值来判定两个方法是否相互构成重载</li>
</ul>
</li>
<li><p>正确范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">double</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="type">float</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a , <span class="type">int</span> b)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>错误范例：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123; 	<span class="comment">/*错误原因：重载与返回值无关*/</span></span><br><span class="line">    	<span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">fn</span><span class="params">(<span class="type">int</span> a)</span> &#123;</span><br><span class="line">        <span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MethodDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">fn</span><span class="params">(<span class="type">double</span> a)</span> &#123; <span class="comment">/*错误原因：这是两个类的两个fn方法*/</span></span><br><span class="line">        <span class="comment">//方法体</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h2 id="7-6-方法的参数传递"   >
          <a href="#7-6-方法的参数传递" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-方法的参数传递" class="headerlink" title="7.6 方法的参数传递"></a>7.6 方法的参数传递</h2>
      
        <h3 id="7-6-1-传递基本类型"   >
          <a href="#7-6-1-传递基本类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-1-传递基本类型" class="headerlink" title="7.6.1  传递基本类型"></a>7.6.1  传递基本类型</h3>
      <ul>
<li><p>测试代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test1</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">         方法参数传递为基本数据类型 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                传入方法中的, 是具体的数值.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法前:&quot;</span> + number);</span><br><span class="line">        change(number);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法后:&quot;</span> + number);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">change</span><span class="params">(<span class="type">int</span> number)</span> &#123;</span><br><span class="line">        number = <span class="number">200</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>
</li>
<li><p>结论：</p>
<ul>
<li><strong>基本数据类型的参数，形式参数的改变，不影响实际参数</strong></li>
</ul>
</li>
<li><p>结论依据：</p>
<ul>
<li>每个方法在栈内存中，都会有独立的栈空间，方法运行结束后就会弹栈消失</li>
</ul>
</li>
</ul>

        <h3 id="7-6-2-传递引用类型"   >
          <a href="#7-6-2-传递引用类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-6-2-传递引用类型" class="headerlink" title="7.6.2 传递引用类型"></a>7.6.2 传递引用类型</h3>
      <ul>
<li><p>测试代码：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test2</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">         方法参数传递为引用数据类型 :</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                传入方法中的, 是内存地址.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>&#125;;</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法前:&quot;</span> + arr[<span class="number">1</span>]);</span><br><span class="line">        change(arr);</span><br><span class="line">        System.out.println(<span class="string">&quot;调用change方法后:&quot;</span> + arr[<span class="number">1</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">change</span><span class="params">(<span class="type">int</span>[] arr)</span> &#123;</span><br><span class="line">        arr[<span class="number">1</span>] = <span class="number">200</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
</li>
<li><p>结论：</p>
<ul>
<li><strong>引用类型的参数，形式参数的改变，影响实际参数的值</strong></li>
</ul>
</li>
<li><p>结论依据：</p>
<ul>
<li>引用数据类型的传参，传入的是地址值，内存中会造成两个引用指向同一个内存的效果，所以即使方法弹栈，堆内存中的数据也已经是改变后的结果</li>
</ul>
</li>
</ul>

        <h1 id="8-进制"   >
          <a href="#8-进制" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-进制" class="headerlink" title="8.进制"></a>8.进制</h1>
      <pre><code>    十进制：Java中，数值默认都是10进制，不需要加任何修饰。
    二进制：数值前面以0b开头，b大小写都可以。
    八进制：数值前面以0开头。
    十六进制：数值前面以0x开头，x大小写都可以。

    注意: 书写的时候, 虽然加入了进制的标识, 但打印在控制台展示的都是十进制数据。
</code></pre>

        <h2 id="8-1-十进制到任意进制的转换"   >
          <a href="#8-1-十进制到任意进制的转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-1-十进制到任意进制的转换" class="headerlink" title="8.1 十进制到任意进制的转换"></a>8.1 十进制到任意进制的转换</h2>
      <ul>
<li>十进制——&gt;二进制</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/rsfln1Me73GES6I.png" alt="1590936221838"></p>
<ul>
<li>十进制——&gt;十六进制</li>
</ul>
<p><img src="https://s2.loli.net/2022/02/12/Y9isNanmwChKfU6.png" alt="1590936342865"></p>
<p>结论：十进制到任意进制的转换</p>
<p>公式：除基取余使用源数据，不断的除以基数（几进制，基数就是几）得到余数，直到商为0，再将余数倒着	拼起来即可</p>

        <h2 id="8-2-原码、补码、反码"   >
          <a href="#8-2-原码、补码、反码" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-2-原码、补码、反码" class="headerlink" title="8.2 原码、补码、反码"></a>8.2 原码、补码、反码</h2>
      <p>**原码 **:就是二进制定点表示法，即最高位为符号位，【0】表示正，【1】表示负，其余位表示数值的大小。</p>
<p><img src="https://s2.loli.net/2022/02/12/fUzRokXqeDAgriL.png" alt="1590936726746"></p>
<p>若符号为为0，则原、反、补一样；</p>
<p>若符号为为1，则，符号为不变，最后面的1以及后面的位保持不变，其余为取反；</p>

        <h2 id="8-3-位运算"   >
          <a href="#8-3-位运算" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-位运算" class="headerlink" title="8.3 位运算"></a>8.3 位运算</h2>
      <p>先将十进制数转成二进制后再进行运算。在二进制位运算中，1表示true，0表示false。</p>

        <h3 id="8-3-1-位运算符介绍"   >
          <a href="#8-3-1-位运算符介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-1-位运算符介绍" class="headerlink" title="8.3.1 位运算符介绍 :"></a>8.3.1 位运算符介绍 :</h3>
      <p><img src="https://s2.loli.net/2022/02/12/hYF5depH4jliuAx.png" alt="1590937235620"></p>

        <h3 id="8-3-2-示例代码"   >
          <a href="#8-3-2-示例代码" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-3-2-示例代码" class="headerlink" title="8.3.2 示例代码"></a>8.3.2 示例代码</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        位运算:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            位运算符指的是二进制位的运算，先将十进制数转成二进制后再进行运算。</span></span><br><span class="line"><span class="comment">            在二进制位运算中，1表示true，0表示false。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             &amp; 位与 : 遇false则false, 遇0则0</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                        00000000 00000000 00000000 00000110     // 6的二进制</span></span><br><span class="line"><span class="comment">                     &amp;  00000000 00000000 00000000 00000010     // 2的二进制</span></span><br><span class="line"><span class="comment">                    -----------------------------------------</span></span><br><span class="line"><span class="comment">                        00000000 00000000 00000000 00000010     // 结果: 2</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             | 位或 : 遇true则true, 遇1则1</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             ^ 位异或 : 相同为false, 不同为true</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">             ~ 取反 : 全部取反, 0变1, 1变0  (也包括符号位)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                    00000000 00000000 00000000 00000110         // 6的二进制补码</span></span><br><span class="line"><span class="comment">                  ~ 11111111 11111111 11111111 11111001</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                  -                                   1         // -1求反码</span></span><br><span class="line"><span class="comment">                   ------------------------------------</span></span><br><span class="line"><span class="comment">                    11111111 11111111 11111111 11111000         // 反码推原码</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                    10000000 00000000 00000000 00000111         // -7</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">6</span> &amp; <span class="number">2</span>);</span><br><span class="line">        System.out.println(~<span class="number">6</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo3</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">       位移运算符:</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">               &lt;&lt; 有符号左移运算，二进制位向左移动, 左边符号位丢弃, 右边补齐0</span></span><br><span class="line"><span class="comment">                        运算规律: 向左移动几位, 就是乘以2的几次幂</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                12 &lt;&lt; 2</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                (0)0000000 00000000 00000000 000011000  // 12的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">       -----------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">               &gt;&gt; 有符号右移运算，二进制位向右移动, 使用符号位进行补位</span></span><br><span class="line"><span class="comment">                        运算规律: 向右移动几位, 就是除以2的几次幂</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                000000000 00000000 00000000 0000001(1)  // 3的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">       -----------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                &gt;&gt;&gt; 无符号右移运算符,  无论符号位是0还是1，都补0</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                                010000000 00000000 00000000 00000110  // -6的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">12</span> &lt;&lt; <span class="number">1</span>);  <span class="comment">// 24</span></span><br><span class="line">        System.out.println(<span class="number">12</span> &lt;&lt; <span class="number">2</span>);  <span class="comment">// 48</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo4</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        ^ 运算符的特点</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                一个数, 被另外一个数, 异或两次, 该数本身不变</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        System.out.println(<span class="number">10</span> ^ <span class="number">5</span> ^ <span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h1 id="9-二维数组概述"   >
          <a href="#9-二维数组概述" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-二维数组概述" class="headerlink" title="9.二维数组概述"></a>9.二维数组概述</h1>
      <p>​	<strong>概述 :</strong>  二维数组也是一种容器，不同于一维数组，该容器存储的都是一维数组容器</p>

        <h2 id="9-1-二维数组动态初始化"   >
          <a href="#9-1-二维数组动态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-1-二维数组动态初始化" class="headerlink" title="9.1 二维数组动态初始化"></a>9.1 二维数组动态初始化</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">动态初始化格式：</span><br><span class="line">数据类型[][] 变量名 = <span class="keyword">new</span> 数据类型[m][n];</span><br><span class="line">m表示这个二维数组，可以存放多少个一维数组</span><br><span class="line">n表示每一个一维数组，可以存放多少个元素</span><br></pre></td></tr></table></div></figure>

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo1Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        动态初始化格式：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            数据类型[][] 变量名 = new 数据类型[m][n];</span></span><br><span class="line"><span class="comment">            m表示这个二维数组，可以存放多少个一维数组</span></span><br><span class="line"><span class="comment">            n表示每一个一维数组，可以存放多少个元素</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 数据类型[][] 变量名 = new 数据类型[m][n];</span></span><br><span class="line">        <span class="type">int</span>[][] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            [[I@10f87f48</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            @ : 分隔符</span></span><br><span class="line"><span class="comment">            10f87f48 : 十六进制内存地址</span></span><br><span class="line"><span class="comment">            I : 数组中存储的数据类型</span></span><br><span class="line"><span class="comment">            [[ : 几个中括号就代表的是几维数组</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            二维数组存储一维数组的时候, 存储的是一维数组的内存地址</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 向二维数组中存储元素</span></span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">0</span>] = <span class="number">11</span>;</span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">1</span>] = <span class="number">22</span>;</span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">2</span>] = <span class="number">33</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从二维数组中取出元素并打印</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="9-2-二维数组访问元素的细节问题"   >
          <a href="#9-2-二维数组访问元素的细节问题" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-2-二维数组访问元素的细节问题" class="headerlink" title="9.2  二维数组访问元素的细节问题"></a>9.2  二维数组访问元素的细节问题</h2>
      <p>问题 :  二维数组中存储的是一维数组, 那能不能存入 [提前创建好的一维数组] 呢 ?</p>
<p>答 : 可以的</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        问题: 二维数组中存储的是一维数组, 那能不能存入 [提前创建好的一维数组] 呢 ?</span></span><br><span class="line"><span class="comment">        答 : 可以的</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr1 = &#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr3 = &#123;<span class="number">77</span>,<span class="number">88</span>,<span class="number">99</span>,<span class="number">100</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">2</span>][<span class="number">3</span>] = <span class="number">100</span>;</span><br><span class="line"></span><br><span class="line">        arr[<span class="number">0</span>] = arr1;</span><br><span class="line">        arr[<span class="number">1</span>] = arr2;</span><br><span class="line">        arr[<span class="number">2</span>] = arr3;</span><br><span class="line"></span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">2</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>][<span class="number">3</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="9-3-二维数组静态初始化"   >
          <a href="#9-3-二维数组静态初始化" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-3-二维数组静态初始化" class="headerlink" title="9.3 二维数组静态初始化"></a>9.3 二维数组静态初始化</h2>
      <pre><code> 完整格式 : 数据类型[][] 变量名 = new 数据类型[][]&#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; 
 
 简化格式 :  数据类型[][] 变量名 = &#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;
</code></pre>
<p>代码实现 : </p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo3Array</span> &#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        完整格式：数据类型[][] 变量名 = new 数据类型[][]&#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        简化格式: 数据类型[][] 变量名 = &#123; &#123;元素1, 元素2...&#125; , &#123;元素1, 元素2...&#125; ...&#125;;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr1 = &#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;;</span><br><span class="line">        <span class="type">int</span>[] arr2 = &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;, &#123;<span class="number">44</span>,<span class="number">55</span>,<span class="number">66</span>&#125;&#125;;</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[][] array = &#123;arr1,arr2&#125;;</span><br><span class="line">        System.out.println(array[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h1 id="10-递归"   >
          <a href="#10-递归" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-递归" class="headerlink" title="10.递归"></a>10.递归</h1>
      <p>递归的介绍：</p>
<ul>
<li>以编程的角度来看，递归指的是方法定义中调用方法本身的现象</li>
<li>把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解</li>
<li>递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算</li>
</ul>
<p>递归的基本使用：</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//案例需求：	用递归求5的阶乘，并把结果在控制台输出</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DiGuiDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//调用方法</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">result</span> <span class="operator">=</span> jc(<span class="number">5</span>);</span><br><span class="line">        <span class="comment">//输出结果</span></span><br><span class="line">        System.out.println(<span class="string">&quot;5的阶乘是：&quot;</span> + result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义一个方法，用于递归求阶乘，参数为一个int类型的变量</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">jc</span><span class="params">(<span class="type">int</span> n)</span> &#123;</span><br><span class="line">        <span class="comment">//在方法内部判断该变量的值是否是1</span></span><br><span class="line">        <span class="keyword">if</span>(n == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="comment">//是：返回1</span></span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//不是：返回n*(n-1)!</span></span><br><span class="line">            <span class="keyword">return</span> n*jc(n-<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>内存图：</p>
<p><img src="https://s2.loli.net/2022/02/13/CHU18JMcmglVviQ.png" alt="递归内存图"></p>
<p>递归的注意事项：</p>
<ul>
<li>递归一定要有出口。否则内存溢出</li>
<li>递归虽然有出口，但是递归的次数也不宜过多。否则内存溢出</li>
</ul>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/2022/02/05/1-Two-Sum/">1.Two_Sum</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2022-02-05</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2022-02-05</span></span></div></header><div class="post-body"><div class="post-excerpt">
        <h1 id="1-两数之和"   >
          <a href="#1-两数之和" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-两数之和" class="headerlink" title="1. 两数之和"></a><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/two-sum/" >1. 两数之和</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></h1>
      <p>给定一个整数数组 <code>nums</code> 和一个整数目标值 <code>target</code>，请你在该数组中找出 <strong>和为目标值</strong> <em><code>target</code></em> 的那 <strong>两个</strong> 整数，并返回它们的数组下标。</p>
<p>你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。</p>
<p>你可以按任意顺序返回答案。</p>

        <h2 id="示例-1："   >
          <a href="#示例-1：" class="heading-link"><i class="fas fa-link"></i></a><a href="#示例-1：" class="headerlink" title="示例 1："></a>示例 1：</h2>
      <figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">输入：nums = [2,7,11,15], target = 9</span><br><span class="line">输出：[0,1]</span><br><span class="line">解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。</span><br></pre></td></tr></table></div></figure>


        <h2 id="示例-2："   >
          <a href="#示例-2：" class="heading-link"><i class="fas fa-link"></i></a><a href="#示例-2：" class="headerlink" title="示例 2："></a>示例 2：</h2>
      <figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：nums = [3,2,4], target = 6</span><br><span class="line">输出：[1,2]</span><br></pre></td></tr></table></div></figure>


        <h2 id="示例-3："   >
          <a href="#示例-3：" class="heading-link"><i class="fas fa-link"></i></a><a href="#示例-3：" class="headerlink" title="示例 3："></a>示例 3：</h2>
      <figure class="highlight plaintext"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：nums = [3,3], target = 6</span><br><span class="line">输出：[0,1]</span><br></pre></td></tr></table></div></figure>


        <h2 id="提示："   >
          <a href="#提示：" class="heading-link"><i class="fas fa-link"></i></a><a href="#提示：" class="headerlink" title="提示："></a>提示：</h2>
      <ul>
<li><code>2 &lt;= nums.length &lt;= 104</code></li>
<li><code>-109 &lt;= nums[i] &lt;= 109</code></li>
<li><code>-109 &lt;= target &lt;= 109</code></li>
<li>只会存在一个有效答案</li>
</ul>
<p>进阶： 你可以想出一个时间复杂度小于 O($N^2$) 的算法吗？</p>

        <h2 id="实现："   >
          <a href="#实现：" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现：" class="headerlink" title="实现："></a>实现：</h2>
      
        <h3 id="解法一-暴力枚举"   >
          <a href="#解法一-暴力枚举" class="heading-link"><i class="fas fa-link"></i></a><a href="#解法一-暴力枚举" class="headerlink" title="解法一:暴力枚举"></a>解法一:暴力枚举</h3>
      <p>直接双循环，每两个元素判断一下。</p>
  <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Solution</span> &#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="type">int</span>[] twoSum(<span class="type">int</span>[] nums, <span class="type">int</span> target) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; nums.length; ++i) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> i + <span class="number">1</span>; j &lt; nums.length; ++j) &#123;</span><br><span class="line">        <span class="keyword">if</span> (nums[i] + nums[j] == target) <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">int</span>[]&#123;i, j&#125;;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">0</span>];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<blockquote>
<p>时间复杂度：O($N^2$)，其中 N 是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。</p>
<p>空间复杂度：O(1)。</p>
</blockquote>

        <h3 id="解法二：哈希表"   >
          <a href="#解法二：哈希表" class="heading-link"><i class="fas fa-link"></i></a><a href="#解法二：哈希表" class="headerlink" title="解法二：哈希表"></a>解法二：哈希表</h3>
      <p>重点是利用哈希表的查询时间为 O(1)的特点，所以只需一次遍历即可，遍历到当前元素时，只需判断<code>num_indx</code>中有没有记录<code>target-nums[i]</code>，若记录过，则找到结果了，直接返回；若没记录过，那就直接把当前元素信息记录进去。</p>
<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Solution</span> &#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="type">int</span>[] twoSum(<span class="type">int</span>[] nums, <span class="type">int</span> target) &#123;</span><br><span class="line">    Map&lt;Integer, Integer&gt; num_index = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; nums.length; ++i) &#123;</span><br><span class="line">      <span class="type">int</span> <span class="variable">otherNum</span> <span class="operator">=</span> target - nums[i];<span class="comment">//需要找的另外一个数</span></span><br><span class="line">      <span class="keyword">if</span> (num_index.containsKey(otherNum)) &#123;<span class="comment">//若记录过，则找到结果了，直接返回</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">int</span>[]&#123;i, num_index.get(otherNum)&#125;;</span><br><span class="line">      &#125;</span><br><span class="line">      num_index.put(nums[i], i);<span class="comment">//若没记录过，那就直接把当前元素信息记录进去</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">0</span>];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<blockquote>
<p>时间复杂度：O(N)，其中 N 是数组中的元素数量。对于每一个元素 <code>nums[i]</code>，我们可以 O(1) 地寻找 <code>target - nums[i]</code>。</p>
<p>空间复杂度：O(N)，其中 N 是数组中的元素数量。主要为哈希表的开销。</p>
</blockquote>
</div></div></article></section><nav class="paginator"><div class="paginator-inner"><span class="page-number current">1</span></div></nav></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><section class="sidebar-toc hide"></section><!-- ov = overview--><section class="sidebar-ov"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/icons/cute.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">星夜兼程</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://gitee.com/bujiuzhi" target="_blank" rel="noopener" data-popover="social.gitee" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">gitee</span></a><a class="sidebar-ov-social-item" href="https://github.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a><a class="sidebar-ov-social-item" href="https://twitter.com/bujiuzhi" target="_blank" rel="noopener" data-popover="Twitter" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-twitter"></i></span></a><a class="sidebar-ov-social-item" href="https://space.bilibili.com/340646348" target="_blank" rel="noopener" data-popover="Youtube" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">bilibili</span></a><a class="sidebar-ov-social-item" href="https://www.zhihu.com/people/bujiuzhi" target="_blank" rel="noopener" data-popover="Zhihu" data-popover-pos="up"><span class="sidebar-ov-social-item__icon">知</span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">7</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">3</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>不久</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v6.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.7.0</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1.0.1/dist/canvas-nest.min.js" color="0,0,0" opacity="0.6" count="99" zIndex="-1"></script><link href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" rel="stylesheet" type="text/css"><link href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/contrib/copy-tex.css" rel="stylesheet" type="text/css"><script src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/contrib/copy-tex.min.js"></script><script src="/js/utils.js?v=2.7.0"></script><script src="/js/stun-boot.js?v=2.7.0"></script><script src="/js/scroll.js?v=2.7.0"></script><script src="/js/header.js?v=2.7.0"></script><script src="/js/sidebar.js?v=2.7.0"></script></body></html>